为了账号安全,请及时绑定邮箱和手机立即绑定

会话未正确结束

会话未正确结束

PHP
隔江千里 2022-10-28 15:37:10
所以通常我的会话应该在我按下页面上的注销按钮时结束,但是当我通过按钮(左上角)转到上一页时。我只是回到我登录的页面..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(); ?>那么,为什么我的会话不能正常工作并且没有完全注销?
查看完整描述

2 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

试试这个!我没有看到您将名称传递给会话的任何地方。


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

    // Initialize the session

       session_start();

    // Unset all of the session variables

       session_unset();

       $_SESSION = array();

    // Destroy the session.

        session_destroy();

        unset($_SESSION['user']);

    // Redirect to login page

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

        exit();

}

注意:我使用了这两个unset()和destroy()功能,你可以使用一个。


查看完整回答
反对 回复 2022-10-28
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

将注销脚本更改为:


<?php


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

    // null the _SESSION

    $_SESSION = null;

    // unset $_SESSION variable for the run-time 

    session_unset();

    // destroy session data in storage

    session_destroy();

    // last, redirect

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

}


?>


查看完整回答
反对 回复 2022-10-28
  • 2 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号