注销不是php

时间:2016-02-17 12:45:25

标签: php logout

我一直在研究一个非常简单的登录系统,以了解事物的基本原理,因此还没有安全性或SQL。所以我现在通过php做这件事。在我的索引页面,我有我的登录等,在我的主页上,我有我的退出等现在我无法让它退出由于某种原因我认为我是在摧毁会话,但我一定是错的。任何人都知道我在哪里出错了。

索引

<!DOCTYPE html>
<?php
        session_start();        
        $username = "admin";
        $password = "collins1";

    if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
        header("Location: home.php");
    }

    if (isset($_POST['username']) && isset($_POST['password'])){
        if ($_POST['username'] == $username && $_POST['password'] == $password)
        {   
            $_SESSION['loggedin'] = true;
            header("Location: home.php");
        }

    }
    else {
        echo "Username or Password incorrect please try again";
    }
?>

<html>
    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Login</title>

        <link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
        <link href="../CSS/master.css" rel="stylesheet" type="text/css">

        <script src="../JAVASCRIPT/respond.min.js"></script>

</head>
<body link="black">
    <div class="gridContainer clearfix">

        <div id="borderDiv">

            <div id="headerDiv">
                <p>Welcome</p>
            </div>

            <div id="subHeaderDiv">
                <p>Please login to continue</p>
            </div>

            <form method="post" action="index.php">
                <div id="userNameLoginDiv">
                    <p>Username:</p>
                    <input type="text" name="username" size="12">
                </div>  

                <div id="userPasswordLoginDiv">
                    <p>Password:</p>
                    <input type="password" name="password" size="12">
                </div>

                <div id="loginBtnDiv">
                    <input id="button" type="submit" value="Login">
                </div>
            </form>

        </div>

    </div>
</body>
</html>

<!DOCTYPE html>
<?php
    session_start();

    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
        header("Location: index.php");
    }

    if (isset($_GET['logout'])){
    session_destroy();  
    }

?>


<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Home</title>
        <link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
        <link href="../CSS/master.css" rel="stylesheet" type="text/css">

        <script src="../JAVASCRIPT/respond.min.js"></script>

    </head>

    <body link="black">

        <div class="gridContainer clearfix">

            <div id="headerDiv">
                <p>Home</p>
            </div>

            <a href="index.php?logout">Logout</a>
        </div>


    </body>
</html>

2 个答案:

答案 0 :(得分:0)

退出链接:index.php?logout

您的index.php不包含session_destroy() - 您需要使用home.php?logout。在您的情况下,您需要在home.php中检查auth之前销毁会话以重定向到index.php

答案 1 :(得分:0)

最佳方式 - 应该有一个退出页面。 logout.php

session_start();
unset($_SESSION['loggedin']);
or
$_SESSION['loggedin']=false;
or session_destory();
header("Location:index.php");
die();