作者 主題: php 問題求救  (閱讀 1700 次)

0 會員 與 1 訪客 正在閱讀本文。

GreenPowder

  • 可愛的小學生
  • *
  • 文章數: 2
    • 檢視個人資料
php 問題求救
« 於: 2022-05-24 20:24 »
各位前輩好,

小弟最近在入門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);
}
?>

twu2

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 5416
  • 性別: 男
    • 檢視個人資料
    • http://blog.teatime.com.tw/1
Re: php 問題求救
« 回覆 #1 於: 2022-05-25 09:31 »
寫 log 或直接 print 相關的變數出來看.
確定傳入的資料與由資料庫取得的資料都跟你想的一樣.

GreenPowder

  • 可愛的小學生
  • *
  • 文章數: 2
    • 檢視個人資料
Re: php 問題求救
« 回覆 #2 於: 2022-05-25 10:54 »
寫 log 或直接 print 相關的變數出來看.
確定傳入的資料與由資料庫取得的資料都跟你想的一樣.

Tommy 大大你好,

感謝你的回覆, 我有用 print將資料庫取得的資料印出來看, password抓出來的值是空值。
我懷疑是查詢資料庫時所下的指令不知道哪裡有問題?

GreenPowder

twu2

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 5416
  • 性別: 男
    • 檢視個人資料
    • http://blog.teatime.com.tw/1
Re: php 問題求救
« 回覆 #3 於: 2022-05-25 11:48 »
用其他的工具去看那資料庫的內容吧. 也許是寫進去就有問題了.