2

我的问题可能看起来很简单,但我不知道我的代码有什么问题。我有一个非常简单的登录系统,如下所示:

登录.php:

<?php  
session_start();

if ($_SESSION['loggedin'] = 1) {
   header("Location: admin.php");
}

if ($_GET['login']) {
     // Only load the code below if the GET
     // variable 'login' is set. You will
     // set this when you submit the form

     if ($_POST['username'] == 'thenemis'
         && $_POST['password'] == 'slustice') {
         // Load code below if both username
         // and password submitted are correct

         $_SESSION['loggedin'] = 1;
          // Set session variable

         header("Location: admin.php");
         exit;
         // Redirect to a protected page

     } else echo "Wrong details";
     // Otherwise, echo the error message
}
?>

<form action="?login=1" method="post" accept-charset="utf-8">  
                <fieldset>
                  <label for="username">Usermame:</label>
                  <input type="text" name="username" placeholder="username" required>
                  <label for="password">Password:</label>
                  <input type="password" name="password" placeholder="password" required>
                  <input type="submit" value="Login"> </td>
                </fieldset>
</form> 

这工作正常。

管理员.php:

<?php

    session_start();
    // Call this function so your page
    // can access session variables

    if ($_SESSION['loggedin'] != 1) {
        // If the 'loggedin' session variable
        // is not equal to 1, then you must
        // not let the user see the page.
        // So, we'll redirect them to the
        // login page (login.php).

        header("Location: login.php");
        exit;
    }

?>

<p><a href="logout.php">Log out</a></p>

现在我的问题是,即使我单击了注销 URL,系统也会让我保持登录状态,如下所示:

注销.php:

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

我的注销程序显然存在一些基本错误,但我似乎找不到它......提前感谢您的帮助!

4

6 回答 6

2

你在这里做任务:

if ($_SESSION['loggedin'] = 1) {
    header("Location: admin.php");
}

你应该做比较

if ($_SESSION['loggedin'] == 1) {
    header("Location: admin.php");
}
于 2013-03-22T10:06:46.003 回答
0

试试这个

<?php
    session_destroy();
    header('Location: index.php');
    exit();
?>
于 2013-03-22T10:00:54.587 回答
0

更改您的admin.php文件

<?php
    session_start();

    if (!isset($_SESSION['loggedin'])) {
        header("Location: login.php");
        exit;
    }

?>

<p><a href="logout.php">Log out</a></p>
于 2013-03-22T10:01:28.547 回答
0

在 login.php 中,您在验证用户详细信息后没有启动 session_start ......

尝试添加 session_start(); 在 $_SESSION['loggedin'] = 1 之前;

这可能对你有用...

在注销.php

在使用此行取消设置会话变量之前

未设置($_SESSION['登录']);

于 2013-03-22T10:01:42.053 回答
0

php.net 手册

为了完全终止会话,例如注销用户,还必须取消设置会话 ID。如果使用 cookie 传播会话 id(默认行为),则必须删除会话 cookie。setcookie() 可以用于此。

使用此代码(从 php.net 复制)安全地注销:

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>
于 2013-03-22T10:04:42.167 回答
0

只需尝试以下更改:

在 login.php 中:

if ($_SESSION['loggedin'] == 1) {
   header("Location: admin.php");
}

在 logout.php 中:

<?php
    session_start();
    ob_start();
    session_destroy();
    $_SESSION['loggedin']=""; //Just empty that session variable
    header("Location: login.php");
?>

我认为这可以帮助您解决问题。

于 2013-03-22T10:24:23.243 回答