0

这是我的登录代码:

<!DOCTYPE = html>
<html lang="nl">
<head>
    <title>
        Bioscoop
    </title>
    <link rel="stylesheet" href="css/style.css"/>
    <?php
        include('config.php');
    ?>
</head>
<body>
    <?php
    if(isset($_POST["login"]))  
    {  
         if(empty($_POST["username"]) || empty($_POST["password"]))  
         {  
              $message = '<label>Vul alle velden in</label>';  
         }  
         else   
         {  
              $query = "SELECT * FROM admins WHERE username = :username AND password = :password";  
              $statement = $con->prepare($query);  
              $statement->execute(  
                   array(  
                        'username'     =>     htmlspecialchars($_POST["username"]),  
                        'password'     =>     htmlspecialchars($_POST["password"])  
                   )  
              );  




              $count = $statement->rowCount();  
              if($count > 0)  
              {  
                   $_SESSION["username"] = $_POST["username"]; 
                   $username = $_SESSION["username"];
                   $query = "SELECT rol FROM admins WHERE username = :username";

                   $stm = $con->prepare($query);
                   $stm->bindParam(':username', $username, PDO::PARAM_STR, 20);
                   $stm->execute();
                   $result = $stm->fetchAll(PDO::FETCH_OBJ);
                   foreach ($result as $pers) {
                       $rol = $pers->rol; 
                       $_SESSION["rol"] = $rol; 
                      if($_SESSION["rol"] == 'admin') {
                          header("location:logged.php");  
                      } else {
                          header("location:login.php");  
                      }
                   } 
              }  
              else  
              {  
                   $message = '<label>Verkeerde Gegevens</label>';  
              }  
         }  
    }  
    ?>
    <header class = "head">
        <img id = "hoofdlogo" src = "img/cinema.png" alt="Logo cinema">
        <p id="copyright">&copy;JopRill</p>
    </header>

    <nav>
        <ul>
            <li><p><a href="index.php"> Home </a></p></li>
            <li><p><a href="films.php"> Films </a></p></li>
            <li><p><a href="#"> Login </a></p></li>
        </ul>
    </nav>

    <form method="post">
                        <div class="form" >
                        <div id="num1"><h2>Login</h2></div>
                            <label><p>Gebruikersnaam</p></label>
                            <input type="text" name="username" id="ContactName" value=""/>
                            <label><p>Wachtwoord</p></label>
                            <input type="password" name="password" id="ContactEmail" value="" />
                            <p></p>
                            <input type="submit" name="login" id="submit" value="Login" />
                            <?php  
                                if(isset($message))  
                                {  
                                    echo '<label class="text-danger">'.$message.'</label>';  
                                }  
                            ?>
                        </div>
                    </form> 
</body>
</html>

这是我登录的页面代码:

<?php
session_start();
?>
<!DOCTYPE = html>
<html lang="nl">
<head>
    <title>
        Bioscoop
    </title>
    <link rel="stylesheet" href="css/style.css"/>

</head>
<body>
<?php
        include('config.php');



    ?>
    <header class = "head">
        <img id = "hoofdlogo" src = "img/cinema.png" alt="Logo cinema">
        <p id="copyright">&copy;JopRill</p>
    </header>

    <nav>
        <ul>
            <li><p><a href="logout.php"> Logout </a></p></li>
        </ul>
    </nav>
    <?php echo $_SESSION["rol"]; ?>
</body>
</html>

在登录页面代码中,我将“角色”保存到会话中。这似乎可行,因为当我单击登录时,我被重定向到logged.php 页面,但是当我尝试回显我用来登录的同一个会话变量时,它说它是未定义的。这是什么原因?

4

1 回答 1

0

要处理$_SESSION变量,您总是需要启动会话session_start();

访问您的网站的访问者被分配一个唯一的 id,即所谓的会话 id。这要么存储在用户端的 cookie 中,要么在 URL 中传播。

会话支持允许您将请求之间的数据存储在 $_SESSION 超全局数组中。当访问者访问您的站点时,PHP 将自动检查(如果 session.auto_start 设置为 1)或根据您的请求(通过session_start()显式)检查是否已随请求发送了特定会话 ID。如果是这种情况,则重新创建先前保存的环境。

请在此处阅读更多信息:http: //php.net/manual/en/intro.session.php

于 2019-01-29T09:59:11.397 回答