Logout.php不会破坏cookie或登录

时间:2013-07-04 05:55:18

标签: php mysql

我有一个logout.php链接,如果用户点击它,则应该将其注销。

<?php
    // logout.php
    // you must start session before destroying it
    session_start();
    session_unset();
    session_destroy();
    //}
    //echo "You have been successfully logged out.
?>

但是,当我返回login.php时 - 它会在login.php之后自动将它们重定向到登录页面。 以下是login.php的代码

<?php 

     // Connects to your Database 

     mysql_connect("localhost", "root", "") or die( mysql_error() ); 
     mysql_select_db("sales") or die( mysql_error() );

    //Checks if there is a login cookie

    if( isset( $_COOKIE['ID_my_site'] ) ) {

        //if there is, it logs you in and directes you to the members page 
        $username = $_COOKIE['ID_my_site']; 
        $pass = $_COOKIE['Key_my_site'];

         $check = mysql_query("SELECT * FROM users WHERE username = '$username'") or die( mysql_error() );
         while( $info = mysql_fetch_array( $check ) ) {
            if ( $pass != $info['password'] ) {
            } else {
              header("Location: sales.php");
            }
         }
     }

     // if login is ok then we add a cookie 
     $_POST['username'] = stripslashes($_POST['username']); 
     $hour = time() + 3600; 
     setcookie( ID_my_site, $_POST['username'], $hour ); 
     setcookie( Key_my_site, $_POST['pass'], $hour );    

     //then redirect them to the members area 

     header("Location: sales.php"); 

3 个答案:

答案 0 :(得分:1)

Cookie会话不是一回事(虽然后者通常由存储在cookie中的标识符支持)。

在cookie中存储用户名和密码是一个糟糕的主意。请改用$_SESSION

答案 1 :(得分:1)

cookiessession不同。 Cookie存储在客户端的浏览器中,而会话存储在服务器中,这可能是您要用于存储usernamepass(在您的情况下)的内容。

如果您切换到使用会话,则logout.php应该可以正常工作,但是如果您希望继续使用Cookie,则应在logout.php中使用以下内容:

setcookie('ID_my_site', ''); 
setcookie('Key_my_site', ''); 

答案 2 :(得分:0)

<?php
// logout.php
// you must start session before destroying it
session_start();
session_unset();
session_destroy();
//}
//echo "You have been successfully logged out.
///to unet the cookie set it to a pas t time
setcookie ("ID_my_site", "", time() - 3600);

?>
相关问题