php - 使用会话登录后显示全名
问题描述
我有这个使用 php、mysqli 和会话的登录系统,它工作正常,但如果我能在登录后在欢迎页面上显示用户全名,那就太好了
我在 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"]
.
推荐阅读
- tensorflow - 在动态 RNN 中设置初始状态
- python - 在 Flask 端点内调用 PySWIP 会使应用程序崩溃
- angular - 如何限制为 1 行编辑?
- machine-learning - 用于生成新编程语言语法的神经网络
- ubuntu - 部署编译软件的更快方法
- ember.js - 如何在 emberjs 中正确构建路由和文件夹?
- google-sheets - 导出带有 TSV 链接的 Google 表格
- gcc - 适合初学者的 ARMv8 汇编编译器错误
- c# - Asp.net Core Web api中的属性路由
- python - 如何将循环的一次迭代限制为以秒为单位的固定时间?