Logout.php不会破坏cookie或登录名


Logout.php does not destroy cookie or login

我有一个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"); 

Cookies是会话,它们不是一回事(尽管后者通常由存储在cookie中的标识符支持)。

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

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

如果你切换到使用会话,那么你的logout.php应该可以正常工作,但如果你想继续使用cookie,你应该在logout.php中使用以下内容:

setcookie('ID_my_site', ''); 
setcookie('Key_my_site', ''); 
<?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);
?>