[英]display full name after login using sessions
我有這個使用 php、mysqli 和 session 的登錄系統,它工作正常,但如果我能在登錄后讓用戶全名顯示在歡迎頁面上,那就太好了
我在 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: user-account.php?user=$username");
exit;
}
// Include config file
require_once "registerconfig.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["user_name"]))){
$username_err = "Please enter username.";
} else{
$username = trim($_POST["user_name"]);
}
// Check if password is empty
if(empty(trim($_POST["user_pass"]))){
$password_err = "Please enter your password.";
} else{
$password = trim($_POST["user_pass"]);
}
// Validate credentials
if(empty($username_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT user_id, user_name, user_pass, customer_name
FROM users WHERE user_name = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Set parameters
$param_username = $username;
$param_customername = $customername;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if username exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $id, $username,
$hashed_password, $customername);
if(mysqli_stmt_fetch($stmt)){
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["user_id"] = $id;
$_SESSION["user_name"] = $username;
$_SESSION["customer_name"] = $customername;
// Redirect user to welcome page
header("location: user-account.php?user_name=$username");
} else{
// Display an error message if password is not valid
$password_err = "The password you entered was not valid.";
}
}
} else{
// Display an error message if username doesn't exist
$username_err = "No account found with that username.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
歡迎頁面代碼如下
<?php echo htmlspecialchars($_SESSION["customer_name"]); ?>
變量$customername
未定義且未分配任何值。 因為這一行$_SESSION["customer_name"] = $customername;
分配給$_SESSION["customer_name"]
空值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.