旧行李 2016-08-30 13:50 采纳率: 25%
浏览 0

不朽的PHP会议

I'm having some issues in destroying a session for a logout. When I destroy the session it keeps coming back.

The code I am using is as follows:

public function kill_login(){
    global $response;
    session_start();
    $response->message = "Current session" . json_encode($_SESSION);
    // Unset all of the session variables.
    unset($_SESSION);
    $_SESSION = array();
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }
    session_destroy();
    session_write_close();

    $response->message .= "Session Destroyed" . json_encode($_SESSION);
}

It replies with below

Returning Session

This is called with ASync AJAX Request then reloads

$("<button>").text("Logout").click(function(){
    var logOut = {
        action:"logOut"
    };
    alert(SAJAX.getJSON(logOut,dataLoc).message);
    location.reload();
})

(SAJAX.getJSON is just a method that simplifies using AJAX for a standard request for mine, I use it a lot just fine)

The page then reloads and somehow the session is still there, I can click the logout button again and still get the same thing.

I only have session_start() used in three places: checking login (establishes session for the page), logon and killing a login since it is an AJAX request.

Any Ideas??

Edit: I adjusted it to remove the second session_start() and add write_close same results.

  • 写回答

1条回答 默认 最新

  • weixin_33736649 2016-08-30 14:21
    关注

    The problem is not in the PHP it is with the call to location.reload(); which will resend the POST data. Since the page was immediately POSTed to by the log-in page the session was being destroyed by the log-out call then a new session was being created due to the POST to the page being resent initiating the login method.

    Changing the location.reload() to window.location.href = "logon.php";resolves the issue since the page was no longer being sent the POST data.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法