2013-03-22 150 views
2

我的问题可能看起来很基本,但我不知道我的代码有什么问题。我有一个非常简单的登录系统,看起来像这样:php注销不工作

的login.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> 

这工作得很好。

admin.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,它看起来像这样:

logout.php:

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

我的注销程序显然有一些基本的错误,但我似乎无法找到它...感谢您提前提供任何帮助!

回答

2

您在这里进行分配:

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

,你应该让comparisment

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

试试这个

<?php 
    session_destroy(); 
    header('Location: index.php'); 
    exit(); 
?> 
0

改变你的admin.php文件

<?php 
    session_start(); 

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

?> 

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

在login.php中后,用户信息验证你没有开始在session_start ...

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

这可能会为你工作...

在logout.php

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

未设置($ _ SESSION [ '的loggedIn']);

0

php.net Manual

为了完全终止会话,喜欢注销用户,会话ID也必须给予取消。如果使用cookie传播会话标识(默认行为),则必须删除会话cookie。 setcookie()可用于此目的。

使用this code(从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(); 
?> 
0

只需尝试以下更改:

在登录。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"); 
?> 

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