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

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

display full name after login using sessions

问题

我有一个使用PHP、MySQLi和会话的登录系统,它可以正常工作,但是在登录后如果能够在欢迎页面上显示用户的全名就更好了。

我在login.php页面上修改了我的代码如下,欢迎页面的代码在登录代码之后,但没有显示全名:

  1. <?php
  2. // 初始化会话
  3. session_start();
  4. // 检查用户是否已经登录,如果已登录则重定向到欢迎页面
  5. if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
  6. header("location: user-account.php?user=$username");
  7. exit;
  8. }
  9. // 包含配置文件
  10. require_once "registerconfig.php";
  11. // 定义变量并初始化为空值
  12. $username = $password = "";
  13. $username_err = $password_err = "";
  14. // 当表单提交时处理表单数据
  15. if($_SERVER["REQUEST_METHOD"] == "POST"){
  16. // 检查用户名是否为空
  17. if(empty(trim($_POST["user_name"]))){
  18. $username_err = "请输入用户名。";
  19. } else{
  20. $username = trim($_POST["user_name"]);
  21. }
  22. // 检查密码是否为空
  23. if(empty(trim($_POST["user_pass"]))){
  24. $password_err = "请输入密码。";
  25. } else{
  26. $password = trim($_POST["user_pass"]);
  27. }
  28. // 验证凭据
  29. if(empty($username_err) && empty($password_err)){
  30. // 准备一个查询语句
  31. $sql = "SELECT user_id, user_name, user_pass, customer_name
  32. FROM users WHERE user_name = ?";
  33. if($stmt = mysqli_prepare($link, $sql)){
  34. // 将变量绑定到准备的语句作为参数
  35. mysqli_stmt_bind_param($stmt, "s", $param_username);
  36. // 设置参数
  37. $param_username = $username;
  38. $param_customername = $customername;
  39. // 尝试执行准备的语句
  40. if(mysqli_stmt_execute($stmt)){
  41. // 存储结果
  42. mysqli_stmt_store_result($stmt);
  43. // 检查用户名是否存在,如果存在则验证密码
  44. if(mysqli_stmt_num_rows($stmt) == 1){
  45. // 绑定结果变量
  46. mysqli_stmt_bind_result($stmt, $id, $username,
  47. $hashed_password, $customername);
  48. if(mysqli_stmt_fetch($stmt)){
  49. if(password_verify($password, $hashed_password)){
  50. // 密码正确,开始新会话
  51. session_start();
  52. // 在会话变量中存储数据
  53. $_SESSION["loggedin"] = true;
  54. $_SESSION["user_id"] = $id;
  55. $_SESSION["user_name"] = $username;
  56. $_SESSION["customer_name"] = $customername;
  57. // 重定向用户到欢迎页面
  58. header("location: user-account.php?user_name=$username");
  59. } else{
  60. // 如果密码无效,则显示错误消息
  61. $password_err = "您输入的密码无效。";
  62. }
  63. }
  64. } else{
  65. // 如果用户名不存在则显示错误消息
  66. $username_err = "找不到具有该用户名的帐户。";
  67. }
  68. } else{
  69. echo "糟糕!出了点问题,请稍后重试。";
  70. }
  71. // 关闭语句
  72. mysqli_stmt_close($stmt);
  73. }
  74. }
  75. // 关闭连接
  76. mysqli_close($link);
  77. }
  78. ?>

欢迎页面的代码如下:

  1. <?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

  1. &lt;?php
  2. // Initialize the session
  3. session_start();
  4. // Check if the user is already logged in, if yes then redirect him to welcome page
  5. if(isset($_SESSION[&quot;loggedin&quot;]) &amp;&amp; $_SESSION[&quot;loggedin&quot;] === true){
  6. header(&quot;location: user-account.php?user=$username&quot;);
  7. exit;
  8. }
  9. // Include config file
  10. require_once &quot;registerconfig.php&quot;;
  11. // Define variables and initialize with empty values
  12. $username = $password = &quot;&quot;;
  13. $username_err = $password_err = &quot;&quot;;
  14. // Processing form data when form is submitted
  15. if($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;){
  16. // Check if username is empty
  17. if(empty(trim($_POST[&quot;user_name&quot;]))){
  18. $username_err = &quot;Please enter username.&quot;;
  19. } else{
  20. $username = trim($_POST[&quot;user_name&quot;]);
  21. }
  22. // Check if password is empty
  23. if(empty(trim($_POST[&quot;user_pass&quot;]))){
  24. $password_err = &quot;Please enter your password.&quot;;
  25. } else{
  26. $password = trim($_POST[&quot;user_pass&quot;]);
  27. }
  28. // Validate credentials
  29. if(empty($username_err) &amp;&amp; empty($password_err)){
  30. // Prepare a select statement
  31. $sql = &quot;SELECT user_id, user_name, user_pass, customer_name
  32. FROM users WHERE user_name = ?&quot;;
  33. if($stmt = mysqli_prepare($link, $sql)){
  34. // Bind variables to the prepared statement as parameters
  35. mysqli_stmt_bind_param($stmt, &quot;s&quot;, $param_username);
  36. // Set parameters
  37. $param_username = $username;
  38. $param_customername = $customername;
  39. // Attempt to execute the prepared statement
  40. if(mysqli_stmt_execute($stmt)){
  41. // Store result
  42. mysqli_stmt_store_result($stmt);
  43. // Check if username exists, if yes then verify password
  44. if(mysqli_stmt_num_rows($stmt) == 1){
  45. // Bind result variables
  46. mysqli_stmt_bind_result($stmt, $id, $username,
  47. $hashed_password, $customername);
  48. if(mysqli_stmt_fetch($stmt)){
  49. if(password_verify($password, $hashed_password)){
  50. // Password is correct, so start a new session
  51. session_start();
  52. // Store data in session variables
  53. $_SESSION[&quot;loggedin&quot;] = true;
  54. $_SESSION[&quot;user_id&quot;] = $id;
  55. $_SESSION[&quot;user_name&quot;] = $username;
  56. $_SESSION[&quot;customer_name&quot;] = $customername;
  57. // Redirect user to welcome page
  58. header(&quot;location: user-account.php?user_name=$username&quot;);
  59. } else{
  60. // Display an error message if password is not valid
  61. $password_err = &quot;The password you entered was not valid.&quot;;
  62. }
  63. }
  64. } else{
  65. // Display an error message if username doesn&#39;t exist
  66. $username_err = &quot;No account found with that username.&quot;;
  67. }
  68. } else{
  69. echo &quot;Oops! Something went wrong. Please try again later.&quot;;
  70. }
  71. // Close statement
  72. mysqli_stmt_close($stmt);
  73. }
  74. // Close connection
  75. mysqli_close($link);
  76. }
  77. ?&gt;

Welcome page code below

  1. &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:

确定