会话未正确结束

所以通常我的会话应该在我按下页面上的注销按钮时结束,但是当我通过按钮(左上角)转到上一页时。我只是回到我登录的页面..


This is my login page code

<?php 


session_start();

$errors = array();

if(isset($_POST["name"]) and isset($_POST["password"])) {

    $conn = mysqli_connect("localhost", "root", "123", "whoosh") or die("No connection made: ".mysqli_connect_error());


    $name = $_POST["name"];

    $password = $_POST["password"];



    if (empty($name)) { array_push($errors, "Ename is required"); }

    if (empty($password)) { array_push($errors, "Password is required"); }


    if (count($errors) == 0) {

        $query = "SELECT * FROM tbl_user WHERE name='$name' AND password='$password'";

        $results = mysqli_query($conn, $query);

        $user = mysqli_fetch_assoc($results);


        if ($user) { // if user exists

        if ($user['name'] === $name and $user['password'] === $password) {

            $_SESSION['user'] = $user['id'];

            header('location: mainsite.php');

        }

    }



    }

}

?>


This is the code I put on my main site thats allows me to logout.

  <?php

    session_start();



   if(isset($_GET['logout'])){

     $_SESSION['name'] = null;

    header('Location:http://leopard.med.agfa.be/leopard/website/logIn.php');

   }


   session_destroy();

 ?>

那么,为什么我的会话不能正常工作并且没有完全注销?


隔江千里
浏览 154回答 2
2回答

HUX布斯

试试这个!我没有看到您将名称传递给会话的任何地方。if(isset($_GET['logout'])){&nbsp; &nbsp; // Initialize the session&nbsp; &nbsp; &nbsp; &nbsp;session_start();&nbsp; &nbsp; // Unset all of the session variables&nbsp; &nbsp; &nbsp; &nbsp;session_unset();&nbsp; &nbsp; &nbsp; &nbsp;$_SESSION = array();&nbsp; &nbsp; // Destroy the session.&nbsp; &nbsp; &nbsp; &nbsp; session_destroy();&nbsp; &nbsp; &nbsp; &nbsp; unset($_SESSION['user']);&nbsp; &nbsp; // Redirect to login page&nbsp; &nbsp; &nbsp; &nbsp; header('Location:http://leopard.med.agfa.be/leopard/website/logIn.php');&nbsp; &nbsp; &nbsp; &nbsp; exit();}注意:我使用了这两个unset()和destroy()功能,你可以使用一个。

尚方宝剑之说

将注销脚本更改为:<?phpif(isset($_GET['logout'])){&nbsp; &nbsp; // null the _SESSION&nbsp; &nbsp; $_SESSION = null;&nbsp; &nbsp; // unset $_SESSION variable for the run-time&nbsp;&nbsp; &nbsp; session_unset();&nbsp; &nbsp; // destroy session data in storage&nbsp; &nbsp; session_destroy();&nbsp; &nbsp; // last, redirect&nbsp; &nbsp; header('Location:http://leopard.med.agfa.be/leopard/website/logIn.php');}?>
打开App,查看更多内容
随时随地看视频慕课网APP