PHP 登录和注销不起作用



我搜索了整个互联网以找到解决方案,但找不到。这是我的问题:我创建了一个网站来登录用户,创建会话并注销(当然(。但是,当我登录用户时,我创建了一个会话,但会话会继续进行,并且在我尝试销毁它时不会停止。

索引.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
    <!-- menu bar -->
    <?php if (session_status() == PHP_SESSION_ACTIVE) { ?>
        <div class="navbar">
            <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="producten.php">Products</a></li>
                <li><a href="FAQ.php">Contact</a></li>
                <li style="cursor: pointer;" onclick="document.getElementById('logoutpop').style.display='block'"><a>My account</a></li>
                <li style="cursor: pointer;"><a href="logout_action.php">Log out</a></li>
                <ul style="float: right;">
                    <li ><a href="winkelmandje.php" >Shopping Bag</a></li>
                </ul>
            </ul>
        </div>
    <?php } elseif (session_status() == PHP_SESSION_NONE){ ?>
        <div class="navbar">
            <ul >
                <li><a href="index.php">Home</a></li>
                <li><a href="producten.php">Products</a></li>
                <li><a href="FAQ.php">Contact</a></li>
                <li style="cursor: pointer;" onclick="document.getElementById('loginpop').style.display='block'"><a>Log in</a></li>
                <ul style="float: right;">
                   <li ><a href="winkelmandje.php" >Shopping bag</a></li>
                </ul>
            </ul>
        </div>
    <?php } else {}?>
    // other irrelevant html code
</body>
</html

login_action.php

<?php
session_start();
// server gegevens
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "db";
// Connect with server
$conn = new mysqli($servername, $username, $password, $dbname);
$email = "";
$password = "";

// Give connection error
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
else {
}
if(isset($_POST['submit'])) {
    $email = $_POST['email'];
    $password = $_POST['password']; 
    // check if filled in
    if(!empty($_POST['email'])) {
        // query in SQL
        $query = ("SELECT * FROM WebsiteUsers WHERE email='$email' AND pass='$password' ");
        // result
        $result = $conn->query($query);
        // check if result exists in database
        if ($result->num_rows > 0) 
        {
                while($row = $result->fetch_assoc()) 
                {
                }
                // session variables
                $_SESSION['loggedin'] = $email;
                $_SESSION['message']="You are now logged in"; 
                header('Location: index.php');
                exit;           
        } 
        else 
        {
             // show some error      
        }               
    }
}
$conn->close(); 

?>

logout_action.php

<?php
session_start();
session_destroy(); 
header("Location: index.php");
exit; 
?>

您正在使用session_status()来检查用户是否已登录。但根据文档 http://php.net/manual/en/function.session-status.php PHP_SESSION_ACTIVE会话开始时是正确的。您在主页的开头创建一个会话session_start()即使用户没有登录,他仍在启动会话。因此,您需要在会话本身中设置一个变量,说明用户是否通过了登录。

也:

  • 您将密码存储在纯文本中,不要使用Bcrypt或不同的算法

  • 您的代码容易受到 SQL 注入的影响,请了解预准备语句并使用它们 http://php.net/manual/en/mysqli.prepare.php 在任何情况下都不要在生产环境中使用当前代码

最新更新