我的PHP会话变量出了什么问题?

时间:2015-10-20 06:34:37

标签: php session

我无法打印$ userLabel($ _SESSION ['nickname'])。我在本地主机上使用phpmyadmin和apache。

我似乎无法弄清楚问题。我在phpmyadmin中创建了一行,我知道它在第4行。这可能是一个错误的方法还是什么?我是PHP的新手,并试图弄清楚。任何解决方案或附加帮助都会很棒!谢谢!

登录:         

if($_POST['submit']) {
    include_once("connection.php");
    $username = strip_tags($_POST['username']);
    $password = strip_tags($_POST['password']);

    $sql = "SELECT id, username, password, nickname FROM users WHERE username = '$username' AND activated = '1' LIMIT 1";
    $query = mysqli_query($connect, $sql);

    if ($query) {
        $row = mysqli_fetch_row($query);
        $userId = $row[0];
        $dbUsername = $row[1];
        $dbPassword = $row[2];
        $userLabel = $row[4];
    }

    if ($username == $dbUsername && $password == $dbPassword) {
        $_SESSION['username'] = $username;
        $_SESSION['id'] = $userId;
        $_SESSION['nickname'] = $userLabel;


        header('Location: user.php');
    } 
    else {
        echo "Error: password mismatch.";
    }
}
?>
<html>
<head>
</head>
<body>
    <form action="index.php" method="post">
        <li>
            <input type="text" name="username" placeholder="Username">
        </li>
        <li>
            <input type="password" name="password" placeholder="Password">
        </li>
        <li>
            <input type="submit" name="submit" value="Sign In">
        </li>
    </form>
</body>
<html>

网页:     

if  (isset($_SESSION['id'])) {
    $userId = $_SESSION['id'];
    $username = $_SESSION['username'];
    $userLabel = $_SESSION['nickname'];
} 
else {
    header('Locaion: index.php');
    die();
}
?>
<html>
<head>
</head>
<body>
    <p><font color="white">Hello <?php echo $userLabel; ?>.</font></
</body>
<html>

1 个答案:

答案 0 :(得分:0)

<?php $userLabel = $row[3]; ?>

<p><font>Hello <?php echo $userLabel; ?>.</font></p>