台灣最大程式設計社群網站
線上人數
1376
 
會員總數:246690
討論主題:190103
歡迎您免費加入會員
討論區列表 >> PHP >> PHP 帳號密碼問題
[]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
PHP 帳號密碼問題
價值 : 5 QP  點閱數:110 回應數:0
樓主

向前行
門外漢
0 5
52 3
發送站內信

各位前輩好,

小弟最近在入門php程式設計, 我做了個會員登入註冊功能, 一開始就遇上一個帳號密碼問題。
底下註冊程式能正確(?)將註冊帳密寫入資料庫, 但登入時就一直說帳密有錯誤! 試了好幾個帳號都一樣!

請問各位是哪裡有問題? 感謝各位指教!

register.php

-----
<?php
// Include config file
require_once "config.php";

// Define variables and initialize with empty values
$account = $password = $confirm_password = "";
$account_err = $password_err = $confirm_password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

// Validate account
if(empty(trim($_POST["account"]))){
$account_err = "Please enter a account.";
} elseif(!preg_match('/^[a-zA-Z0-9_]+$/', trim($_POST["account"]))){
$account_err = "Username can only contain letters, numbers, and underscores.";
} else{
// Prepare a select statement
$sql = "SELECT id FROM member WHERE account = ?";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_account);

// Set parameters
$param_account = trim($_POST["account"]);

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
/* store result */
mysqli_stmt_store_result($stmt);

if(mysqli_stmt_num_rows($stmt) == 1){
$account_err = "This username is already taken.";
} else{
$account = trim($_POST["account"]);
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}

// Close statement
mysqli_stmt_close($stmt);
}
}

// Validate password
if(empty(trim($_POST["password"]))){
$password_err = "Please enter a password.";
} elseif(strlen(trim($_POST["password"])) < 6){
$password_err = "Password must have atleast 6 characters.";
} else{
$password = trim($_POST["password"]);
}

// Validate confirm password
if(empty(trim($_POST["confirm_password"]))){
$confirm_password_err = "Please confirm password.";
} else{
$confirm_password = trim($_POST["confirm_password"]);
if(empty($password_err) && ($password != $confirm_password)){
$confirm_password_err = "Password did not match.";
}
}

// Check input errors before inserting in database
if(empty($account_err) && empty($password_err) && empty($confirm_password_err)){

// Prepare an insert statement
$sql = "INSERT INTO member(account, password) VALUES (?, ?)";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_account, $param_password);

// Set parameters
$param_account = $account;
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
header("location: login.php");
} else{
echo "Oops! Something went wrong. Please try again later.";
}

// Close statement
mysqli_stmt_close($stmt);
}
}

// Close connection
mysqli_close($link);
}
?>

login.php
-----
<?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
$account = $password = "";
$account_err = $password_err = $login_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

// Check if account is empty
if(empty(trim($_POST["account"]))){
$account_err = "Please enter username.";
} else{
$account = trim($_POST["account"]);
}

// 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($account_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT id, account, password FROM member WHERE account = ?";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_account);

// Set parameters
$param_account = $account;

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);

// Check if account exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $id, $account, $hashed_password);
if(mysqli_stmt_fetch($stmt)){
echo "Password=" . $password;
if(password_verify($password, $hashed_password)){
// Password is correct, so start a new session
session_start();

// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["account"] = $account;

// Redirect user to welcome page
header("location: welcome.php");
} else{
// Password is not valid, display a generic error message
$login_err = "帳號或密碼錯誤.";
}
}
} else{
// account doesn't exist, display a generic error message
$login_err = "Invalid account or password.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}

// Close statement
mysqli_stmt_close($stmt);
}
}

// Close connection
mysqli_close($link);
}
?>






搜尋相關Tags的文章: [ 帳號密碼 ] ,
本篇文章發表於2022-05-03 12:59
目前尚無任何回覆
   

回覆
如要回應,請先登入.