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

标签 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"); 

最佳答案

Cookie 和 session 不是同一件事(尽管后者通常由存储在 Cookie 中的标识符支持)。

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

关于php - Logout.php 不会破坏 cookie 或登录名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17462816/

相关文章:

php - 由于表锁定,ajax 未完成进程

java - 如何通过php在Xampp中执行jar文件

javascript - 如何通过单击链接更新 MySQL 数据库行?

php - 为什么我会收到此编译器错误。语法对我来说是正确的

Mysql表锁代替行锁

mysql - 在 TypeScript 中导入时“找不到模块”

php - 防止 PHP 中的 DoS 等暴力攻击

php - Drupal php 查询不适用于某些单词

php - 字符编码问题?

PHP 从 MySQL 获取下一条/上一条记录