我正在使用 Mac OS、mamp、phpmyadmin 创建一个简单的登录表单。我在 php myadmin 和第一个用户中创建了我的数据库。我确定用户名和密码正确(根据我的数据库),但我收到密码不正确的错误。
我松散地遵循本教程https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
我相信这可能会导致错误,但我不确定。
$hashed_password = $row["password"];
if(password_verify($password, $hashed_password)){
<?php
// Initialize the session
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: welcome.php");
exit;
}
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if username is empty
if(empty(trim($_POST["username"]))){
$username_err = "Please enter username.";
} else{
$username = trim($_POST["username"]);
}
// Check if password is empty
if(empty(trim($_POST["password"]))){
$password_err = "Please enter your password.";
} else{
$password = trim($_POST["password"]);
}
// Validate credentials
if(empty($username_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT id, username, password FROM users WHERE username = :username";
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
// Set parameters
$param_username = trim($_POST["username"]);
?>
我希望被带到我的欢迎屏幕。
蓝山帝景
斯蒂芬大帝