-1

所以我试图在用户登录后显示用户名,但我无法让它工作......这是我正在使用的代码:

首先,用户必须登录。

  <div>
     <form  method="POST" action="login.php">
        <h2>LOGIN</h2>
        <input type="text"  placeholder="Username" name="username" />
        <input type="password" placeholder="Password" name="password" />

        <br />

        <input class="btn btn-large btn-primary" type="submit" name="login" value="Log In" />
     </form>
  </div>

使用表单后,将加载此脚本,这是“login.php”:

<?php

session_start();
if(!$_SERVER['REQUEST_METHOD'] == 'POST') {
    echo "Go away.<br /><br />";
    exit(); 
}


        if(mysql_connect('localhost', 'root', '')) {
    if(mysql_select_db('users')) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
        $zoekresultaat = mysql_query($query);

        if($zoekresultaat = mysql_affected_rows() > 0) {
            $record = mysql_fetch_assoc($zoekresultaat);

            $_SESSION['login'] = true;
            $_SESSION['username'] = $record['username'];

            header('location: dashboard.php');
            } else {
                header('location: loginerror.php');
            }
            exit();
    } exit(); {
        echo "Can't find database.";
    }
} else {
    echo "Can't connect to database.";
}

?>

完成后,用户将被重定向到“dashboard.php”,如下所示:

<?php include "includes/menu.php";?>


<div id='content'>
<h1><strong>Welkom <?php echo $_SESSION['username']; ?></strong></h1>
<p></p>
</div>


<?php include "includes/foot.php";?>

问题是当我尝试登录时出现以下错误:

注意:未定义变量:第 5 行 D:\xampp\htdocs\NGP\dashboard.php 中的 _SESSION

我真的很迷茫,希望能在这里得到一些答案!提前致谢!

4

1 回答 1

3

只需包含 session_start(); 公共包含文件中的行。

于 2013-07-09T09:28:40.383 回答