在单个$ _SESSION(PHP)中存储多个变量

时间:2012-05-25 04:11:07

标签: php session login

我怎么做这项工作,我之前问过,没有得到正确答案。此代码是用户登录,因此当他们登录时,我希望用户名和头像可以通过网站进行跟踪。到目前为止我只有用户名。我已经尝试了方法并且每次都失败了。

$username = $_POST['username'];
$password = sha1($_POST['password']);
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
$count=mysqli_num_rows($result);

if ($count == 1) 
{
   $row = mysqli_fetch_array($result);
   while ($_SESSION['username'] = $row['username'])
   {
      session_start();
      header('Location: index.php'); 
   }
}
else
{
   echo 'Invalid Logins';
}

mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:2)

假设您的头像存储在数据库的avatar字段中:

if ($count == 1) 
{
    session_start();

    $row = mysqli_fetch_array($result);
    $_SESSION['username'] = $row['username'];
    $_SESSION['avatar'] = $row['avatar'];

    header('Location: index.php'); 
}
else
{
    echo 'Invalid Logins';
}