使用会话登录后显示完整姓名

huangapple go评论95阅读模式
英文:

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

&lt;?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[&quot;loggedin&quot;]) &amp;&amp; $_SESSION[&quot;loggedin&quot;] === true){
header(&quot;location: user-account.php?user=$username&quot;);
exit;
}
// Include config file
require_once &quot;registerconfig.php&quot;;
// Define variables and initialize with empty values
$username = $password = &quot;&quot;;
$username_err = $password_err = &quot;&quot;;
// Processing form data when form is submitted
if($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;){
// Check if username is empty
if(empty(trim($_POST[&quot;user_name&quot;]))){
$username_err = &quot;Please enter username.&quot;;
} else{
$username = trim($_POST[&quot;user_name&quot;]);
}
// Check if password is empty
if(empty(trim($_POST[&quot;user_pass&quot;]))){
$password_err = &quot;Please enter your password.&quot;;
} else{
$password = trim($_POST[&quot;user_pass&quot;]);
}
// Validate credentials
if(empty($username_err) &amp;&amp; empty($password_err)){
// Prepare a select statement
$sql = &quot;SELECT user_id, user_name, user_pass, customer_name 
FROM users WHERE user_name = ?&quot;;
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, &quot;s&quot;, $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[&quot;loggedin&quot;] = true;
$_SESSION[&quot;user_id&quot;] = $id;
$_SESSION[&quot;user_name&quot;] = $username;
$_SESSION[&quot;customer_name&quot;] = $customername;
// Redirect user to welcome page
header(&quot;location: user-account.php?user_name=$username&quot;);                            
} else{
// Display an error message if password is not valid
$password_err = &quot;The password you entered was not valid.&quot;;
} 
}
} else{
// Display an error message if username doesn&#39;t exist
$username_err = &quot;No account found with that username.&quot;;
}
} else{
echo &quot;Oops! Something went wrong. Please try again later.&quot;;
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?&gt;

Welcome page code below

&lt;?php echo htmlspecialchars($_SESSION[&quot;customer_name&quot;]); ?&gt;

答案1

得分: 1

Variable $customername 未定义,且未为其分配任何值。因此,在$_SESSION[&quot;customer_name&quot;] = $customername;这一行中,将空值赋给了$_SESSION[&quot;customer_name&quot;]

英文:

Variable $customername is not defined and no value assigned to it. Because of that on line $_SESSION[&quot;customer_name&quot;] = $customername; empty value assigned to $_SESSION[&quot;customer_name&quot;].

huangapple
  • 本文由 发表于 2020年1月3日 19:15:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定