draxq02664 2018-08-12 03:36
浏览 81

无法确定PHP中的会话何时处于活动状态

So I want to be able to tell, in PHP, whether a session is currently active. I thought that this would be easy by either using session_status() or session_id(), but both return inconclusive results.

My PHP looks like this

if (session_id() == "") {
    echo json_encode("nope");
} else {
    echo json_encode("yep");
}

if (session_status() === PHP_SESSION_DISABLED) {
    echo json_encode("disabled");
} elseif (session_status() === PHP_SESSION_NONE) {
    echo json_encode("none");
} else {
    echo json_encode("active");
}

$handler = new DatabaseSessionHandler();
session_set_save_handler($handler, true);
session_start();

And I have an Angular app making http calls to this script to basically activate its session.

I've been testing this, and when I visit the site for the first time, no session_id is found and the session_status() returns back "none", as they both should. But the problem is, when I refresh the page, and this PHP script is runs, but this time I have an active session (the PHP session cookie shows up in my browser), session_id is still acting like none exists. And also, session_status() returns back, mistakenly "none".

Any ideas would be greatly appreciated, thanks!

Edit 1: A few people have mentioned that putting the session_start() in front of testing if it is active or not should work. When I do this, a session_id is always found, and the session_status() always returns back "active". Even when a fresh new user visits the site, this still happens.

  • 写回答

3条回答 默认 最新

  • dongzhang7157 2018-08-12 06:46
    关注

    I am not sure if this will solve your problem, but you could check if your session directory is writable:

    if (!is_writable(session_save_path())) {
        echo 'Session path "'.session_save_path().'" is not writable for PHP!'; 
    }
    

    A question related to that is at PHP Session not Saving.

    Elsewhere try the following:

    if (session_id() == "") {
        session_start();
        echo json_encode("nope");
    } else {
        echo json_encode("yep");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度