在php中注销后如何防止回到主页

时间:2015-07-25 02:46:12

标签: login

这些是我登录的代码并注销。它适用于登录bt而不是注销。它在退出后返回主页。请帮助我这个

<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
session_start();
if ($_SESSION["logged_in"] == "yes") {
    echo "<script>window.location='ShowRecentVisit.php';</script>";
    exit();
}
?>

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/>
        <link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    </head>
    <body>

        <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script src="js/materialize.js"></script>
        <script type="text/javascript">
            function test() {
                window.location = "LoginPage.html";
            }
        </script>
        <nav class="materialize-red">
            <div class="nav-wrapper">
                <a href="#" class="brand-logo center">Logo</a>
                <ul style="margin-left:10%;" id="nav-mobile" class="center hide-on-med-and-down">
                    <li class="waves-effect waves-ripple waves-light">
                        <a href="sass.html">Sass</a>
                    </li>
                    <li class="waves-effect waves-ripple waves-light">
                        <a href="badges.html">Components</a>
                    </li>
                    <li class="waves-effect waves-ripple waves-light">
                        <a href="collapsible.html">JavaScript</a>
                    </li>
                </ul>
            </div>
        </nav>
        <div class="container z-depth-1">
            <form action="UserLogin.php" method="post">
                <table class="centered">
                    <tr>
                        <td>Email</td>
                        <td>
                        <input type="text" name="userMail" />
                        </td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td>
                        <input type="password" name="userPass" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        <button type="submit" class="btn waves-effect waves-ripple">
                            Login
                        </button></td>
                        <td>
                        <button type="button" class="btn waves-effect waves-ripple" id="btn1" onclick="test()">
                            Sign Up
                        </button></td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
    </html>

这是登录控制页面。登录是完美的。 //这是UserLogin.php

<?php
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Expires: Fri, 06 Jun 1975 15:10:00 GMT');
include ("dbConfig.php");
session_start();
$userMail = $_POST['userMail'];
$userPassword = $_POST['userPass'];
$userPassword = md5($userPassword);

$sql = "SELECT * FROM userinfo WHERE Usergmail = '$userMail';";

$result = mysql_query($sql);

$row = mysql_fetch_row($result);

if ($row['1'] == $userPassword) {
    $_SESSION["logged_in"] = "yes";
    $_SESSION["save_url"] = $_POST['userMail'];
    header("location: ShowRecentVisit.php");
    exit();
} else {
    echo "<script>window.location='http://www.youtube.com';</script>";

}
?>

在这一部分。退出是好的。 bt我可以在退出后返回主页。我怎么能解决它 //这是logout.php

<?php
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Expires: Fri, 06 Jun 1975 15:10:00 GMT');
session_start();

if (session_destroy()) {
    $_SESSION = array();
    header("location: LoginPage.php");
}
?>

1 个答案:

答案 0 :(得分:0)

如果我做对了,你需要在ShowRecentVisit.php上实现一个访问控制,以便检查用户是否经过身份验证。

您不仅需要在此文件上执行此操作,还需要在阻止访客(未经过身份验证)访问所需的任何其他文件上执行此操作。

也许把这段代码放在文件的开头:

if ( ! isset($_SESSION["logged_in"]) || $_SESSION["logged_in"] != "yes") {
    echo "<script>window.location='GUEST_PAGE.php';</script>";
    exit();
}

您可以使用此代码创建一个文件,并在需要的位置上使用它,这样您就可以更轻松地进行维护。