0

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

3 回答 3

1

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

如果您切换到使用会话,那么您logout.php应该可以正常工作,但是如果您希望继续使用 cookie,您应该在您的logout.php:

setcookie('ID_my_site', ''); 
setcookie('Key_my_site', ''); 
于 2013-07-04T06:03:02.393 回答
1

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

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

于 2013-07-04T05:59:06.543 回答
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);

?>
于 2013-07-04T06:12:27.040 回答