在PHP中删除$ _SESSION的问题

时间:2018-04-03 13:56:16

标签: php html ajax

我在删除会话时遇到问题。它存储在cookie中,每当我点击退出按钮时,都没有任何反应。

HTML

<input id="log_out_button" onclick="logout()" type="button" value="Log Out">

AJAX

function logout() {
    // Create request object 
    var request = new XMLHttpRequest();
    // Create event handler that specifies what should happen when server responds
    request.onload = function() {
        // Check HTTP status code
        if(request.status == 200) {
            document.getElementById("error_messages").innerHTML = "";
        }
    else
        alert("Error communicating with server: " + request.status);
}
// Set up request with HTTP method and URL 
request.open("GET", "php/log_out.php");
//Send request
request.send();
}

PHP

<?php
//Start session management
session_start();

//Remove all session variables
session_unset(); 

//Destroy the session 
session_destroy();
?>

1 个答案:

答案 0 :(得分:1)

  

session_start()根据通过GET或POST请求传递的会话标识符创建会话或恢复当前会话,或通过cookie传递。

所以如果你想关闭用户会话,你还需要清除cookie

setcookie(session_name(), false, -1, '/');

http://php.net/manual/en/function.setcookie.php

http://php.net/manual/en/function.session-start.php