英文:
display full name after login using sessions
问题
我有一个使用PHP、MySQLi和会话的登录系统,它可以正常工作,但是在登录后如果能够在欢迎页面上显示用户的全名就更好了。
我在login.php页面上修改了我的代码如下,欢迎页面的代码在登录代码之后,但没有显示全名:
<?php
// 初始化会话
session_start();
// 检查用户是否已经登录,如果已登录则重定向到欢迎页面
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: user-account.php?user=$username");
exit;
}
// 包含配置文件
require_once "registerconfig.php";
// 定义变量并初始化为空值
$username = $password = "";
$username_err = $password_err = "";
// 当表单提交时处理表单数据
if($_SERVER["REQUEST_METHOD"] == "POST"){
// 检查用户名是否为空
if(empty(trim($_POST["user_name"]))){
$username_err = "请输入用户名。";
} else{
$username = trim($_POST["user_name"]);
}
// 检查密码是否为空
if(empty(trim($_POST["user_pass"]))){
$password_err = "请输入密码。";
} else{
$password = trim($_POST["user_pass"]);
}
// 验证凭据
if(empty($username_err) && empty($password_err)){
// 准备一个查询语句
$sql = "SELECT user_id, user_name, user_pass, customer_name
FROM users WHERE user_name = ?";
if($stmt = mysqli_prepare($link, $sql)){
// 将变量绑定到准备的语句作为参数
mysqli_stmt_bind_param($stmt, "s", $param_username);
// 设置参数
$param_username = $username;
$param_customername = $customername;
// 尝试执行准备的语句
if(mysqli_stmt_execute($stmt)){
// 存储结果
mysqli_stmt_store_result($stmt);
// 检查用户名是否存在,如果存在则验证密码
if(mysqli_stmt_num_rows($stmt) == 1){
// 绑定结果变量
mysqli_stmt_bind_result($stmt, $id, $username,
$hashed_password, $customername);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
// 密码正确,开始新会话
session_start();
// 在会话变量中存储数据
$_SESSION["loggedin"] = true;
$_SESSION["user_id"] = $id;
$_SESSION["user_name"] = $username;
$_SESSION["customer_name"] = $customername;
// 重定向用户到欢迎页面
header("location: user-account.php?user_name=$username");
} else{
// 如果密码无效,则显示错误消息
$password_err = "您输入的密码无效。";
}
}
} else{
// 如果用户名不存在则显示错误消息
$username_err = "找不到具有该用户名的帐户。";
}
} else{
echo "糟糕!出了点问题,请稍后重试。";
}
// 关闭语句
mysqli_stmt_close($stmt);
}
}
// 关闭连接
mysqli_close($link);
}
?>
欢迎页面的代码如下:
<?php echo htmlspecialchars($_SESSION["customer_name"]); ?>
请确保在欢迎页面的代码中使用正确的会话变量名,以便正确显示用户的全名。
英文:
I have this login system using php, mysqli and sessions and it works fine but would be good if I can get the users full name displayed on the welcome page after login
I amended my code to the following on the login.php page and the welcome page code is after the login code but is not showing the full name
<?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);
}
?>
Welcome page code below
<?php echo htmlspecialchars($_SESSION["customer_name"]); ?>
答案1
得分: 1
Variable $customername
未定义,且未为其分配任何值。因此,在$_SESSION["customer_name"] = $customername;
这一行中,将空值赋给了$_SESSION["customer_name"]
。
英文:
Variable $customername
is not defined and no value assigned to it. Because of that on line $_SESSION["customer_name"] = $customername;
empty value assigned to $_SESSION["customer_name"]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论