session_status() 在重新加载时不返回 [英] session_status() returns none on reload
问题描述
我已经看过这个 相同 相反 问题及其 OP 代码示例对我来说似乎非常好.我也读过(包括 PHP 文档)可以在 PHP >= 5.4 上使用 session_status()
检查会话,在调用 session_start()
之前这样做应该没问题code> 以确定它是否已经存在.
I have already seen this identical opposite question and its OP code sample seems perfectly fine to me. I've also read (including PHP docs) that session can be checked with session_status()
on PHP >= 5.4 and it should be fine to do so before calling session_start()
to determine if it already exists.
现在,我在 CentOS 7.10 机器上使用 PHP 5.4.16 并且 session_status()
总是为我返回 1 (PHP_SESSION_NONE)
,只有在我重新加载时带有此示例的页面:
Now, I'm using PHP 5.4.16 on a CentOS 7.10 machine and the session_status()
always returns 1 (PHP_SESSION_NONE)
for me, only when I reload the page with this example:
<?php
$status = session_status();
session_start();
echo "The session status is : $status";
我希望它在我重新加载页面时返回 PHP_SESSION_ACTIVE (2)
(并且我没有在清除缓存的情况下强制重新加载).我使用的是最新的 Chrome 版本,页面嵌入在一个虚拟主机中,通过端口 8443 使用 HTTPS.
I expect it returns PHP_SESSION_ACTIVE (2)
when I reload page (and I'm not forcing-reload with cache clear). I'm using latest Chrome version, and the page is embedded inside a virtual host with HTTPS over port 8443.
我已经使用 php --version
和 phpinfo()
脚本检查了 PHP 版本,以丢弃任何版本冲突以防万一,它们是相同的.此外,当我访问会话脚本页面时,服务器会在 /var/lib/php/session
目录中创建空文件.我没有更改会话或任何内容的默认 PHP 设置.
I've checked the PHP version with both php --version
and via phpinfo()
script to discard any version conflict just in case, and they're same. Additionally, when I visit the session script page, the server creates empty files at /var/lib/php/session
directory. I didn't change default PHP settings for session or anything.
说清楚:session_status()
如果之后在同一个脚本执行时检查,则工作正常(会话状态为:2
),但自然我不是对此感兴趣,因为我想首先检查会话是否存在.
To make it clear: the session_status()
works fine if checked afterwards on same script execution (The session status is : 2
), but naturally I'm not interested in that since I want to check if session exist in first place.
可能有什么问题?
推荐答案
您的 session_status
将始终为 PHP_SESSION_NONE,直到您在当前请求的生命周期内调用 session_start.如果您在之前的某个请求中已经开始",这并不重要.一个会话:你需要重新开始"它,恢复以前开始的会话.这是通过 cookie 完成的.
Your session_status
will always be PHP_SESSION_NONE until you call session_start in the lifetime of the current request. It does not matter if on some previous request you "already started" a session: you need to "re-start" it, to resume a previously started session. This is done via cookies.
如果您的意图是仅当用户过去已经执行了启动会话的某些操作(例如,他们之前已经登录)并且您想恢复会话但不想开始新会话时才开始会话,那么您可以简单地检查 if (isset($_COOKIE['PHPSESSID']))
.
If your intent is to start a session only if the user already did something in the past that started a session (eg. they already logged in previously) and you want to resume a session but not start a new one, then you could simply check if (isset($_COOKIE['PHPSESSID']))
.
如果您知道大多数用户不需要会话,并且您希望避免 cookie 和会话所涉及的开销,但仍然能够处理需要会话的情况,这可能会很有用.
This may be beneficial if you know most of your users won't need a session, and you want to avoid cookies and the overhead involved with a session, but still be able to handle the cases when a session is needed.
这篇关于session_status() 在重新加载时不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!