使用会话从数据库中检索有关登录用户的信息

时间:2014-08-05 21:15:09

标签: php database session mysqli

好的基本上 - 我正在尝试显示用户的名字。因此,当他们登录他们的个人资料时,它会显示'Hello [Person First Name!]。为此,我尝试了

echo $_SESSION['fname'];

我知道我必须在下面的脚本中设置$ fname。有人可以看一下,因为我真的想要做到这一点!谢谢!

<?php

session_start();

$connect = mysqli_connect("localhost", "root", "root", "EmilysCakes");

if(isset($_POST['login']) &&
    isset($_POST['username']) && 
    isset($_POST['password']) &&
            $_POST['username']!='' &&
            $_POST['password']!=''){

                $pass = $_POST['password'];
                $passMD5 = md5($pass);
                $user = $_POST['username'];

            $q = mysqli_query($connect, "SELECT * FROM `members` WHERE `username`='$user'");
            if(mysqli_num_rows($q) > 0) {
                $info = mysqli_fetch_array($q);
                $storedPassword = $info['password'];
                if($storedPassword==$passMD5){

                        // Retrieves the information of the user
                        $_SESSION['username'] = $user;
                        $_SESSION['fname'] = $fname;
                        $_SESSION['lname'] = $lname;
                        $_SESSION['email'] = $email;

                        header("location:../views/profile.php");
                        exit();
                }
                else
                    echo "Password is incorrect. Please try again.";
                }else
                    echo "That username was not found. Please try again.";
                }


?>

2 个答案:

答案 0 :(得分:1)

试试这段代码

 <?php     
 //....
 $_SESSION['username'] = $info['user'];
 $_SESSION['fname'] = $info['fname'];
 $_SESSION['lname'] = $info['lname'];
 $_SESSION['email'] = $info['email'];

 //....
 ?>

然后,您可以通过echo $ _SESSION [&#39;用户名&#39;]

在您网页的任何位置拨打会话[&#39;用户名&#39;]

答案 1 :(得分:1)

不完全确定您尝试做什么,但我认为您正在寻找此而不是您目前所拥有的

$user  = $_SESSION['username'] = $info['username'];
$fname = $_SESSION['fname']    = $info['fname'];
$lname = $_SESSION['lname']    = $info['lname'];
$email = $_SESSION['email']    = $info['email'];

echo "Hello " $fname;