为了账号安全,请及时绑定邮箱和手机立即绑定

用户登录并注册后如何显示“名称”和“电子邮件”之类的数据

用户登录并注册后如何显示“名称”和“电子邮件”之类的数据

PHP
白猪掌柜的 2021-05-10 09:22:25
我是编码的新手。我正在尝试在接下来的几页上显示用户已保存(注册时)的某些数据。我可以显示用户名,但也要显示名称和电子邮件。这是用户登录时的时间。    $query = "SELECT * FROM users                 WHERE username='$username'                 AND password='$password'";    $results = mysqli_query($db, $query);    if (mysqli_num_rows($results) == 1) {        $_SESSION['username'] = $username;        $_SESSION['success'] = "You are now logged in";        header('location: indexclient.php');    }else {        array_push($errors, "Wrong username/password combination");    }    //new page    //This is the top of the page where I want to display the name.    session_start();    if (!isset($_SESSION['username'])) {        $_SESSION['msg'] = "You must log in first";        header('location: loginclient.php');    }    if (isset($_GET['logout'])) {        session_destroy();        unset($_SESSION['username']);        header("location: loginclient.php");    }    //This is where I want to display the name at the bottom of the page:  <h2> <?php echo $_SESSION['username']; ?>'s Profile</h2>  <?php echo $_SESSION['firstname']; ?>  <?php  if (isset($_SESSION['username'])) {   echo "Hello",  $_SESSION['firstname']  ;  }
查看完整描述

2 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

您需要从数据库获取数据并将其存储在变量中,以便可以在任何需要的地方使用它们。


$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";

$results = mysqli_query($db, $query);

while($data = mysqli_fetch_array($results)) {

  $name = $data['name_from_db']; //Here you put a name of column where you store name in DB

  $email = $data['email_from_db']; //Here you put a name of column where you store email in DB


 // You can put in session if you want to have it globaly

}


if (mysqli_num_rows($results) == 1) {

    $_SESSION['username'] = $username;

    $_SESSION['success'] = "You are now logged in";

    $_SESSION['name'] = $name ; //This is example where I put in SESSION variable

    $_SESSION['email'] = $email; //This is example where I put in SESSION variable

    header('location: indexclient.php');

} else {

    array_push($errors, "Wrong username/password combination");

}

请注意,在此代码中,您没有为安全做任何事情,它很容易受到攻击。请阅读有关参数化查询和防止SQL注入的保护,以及有关密码哈希的信息。用纯文本发送密码是不安全的!


查看完整回答
反对 回复 2021-05-14
?
隔江千里

TA贡献1906条经验 获得超10个赞

在此部分:


if (mysqli_num_rows($results) == 1) {

    $_SESSION['username'] = $username;

    //Add another entry in SESSION here

    $_SESSION['success'] = "You are now logged in";

    header('location: indexclient.php');

}

您可以添加一个新条目,例如 $_SESSION['email'] = $result[index_of_email_in_results]


查看完整回答
反对 回复 2021-05-14
  • 2 回答
  • 0 关注
  • 267 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信