简体   繁体   English

如何判断会话是否有效?

[英]How to tell if a session is active?

Per request, there are a few different ways that you can tell whether or not a session has been started, such as: 根据请求,您可以通过几种不同的方式判断会话是否已启动,例如:

$isSessionActive = (session_id() != "");

Or: 要么:

$isSessionActive = defined('SID');

However, these both fail if you start a session, then close it; 但是,如果你开始一个会话,然后关闭它们,它们都会失败; session_id() will return the prior session's ID, while SID will be defined. session_id()将返回先前会话的ID,同时将定义SID Likewise, calling session_start() at this point will generate an E_NOTICE if you already have a session active. 同样,如果您已经激活会话,此时调用session_start()将生成E_NOTICE Is there a sane way to check if a session is currently active, without having to resort to output buffering, the shut-up operator ( @session_start() ), or something else equally as hacky? 是否有一种理智的方法来检查会话当前是否处于活动状态,而不必求助于输出缓冲,关闭运算符( @session_start() )或其他类似hacky的东西?

EDIT: I wrote a patch to try to get this functionality included in PHP: http://bugs.php.net/bug.php?id=52982 编辑:我写了一个补丁试图让这个功能包含在PHP中: http//bugs.php.net/bug.php?id = 52982

EDIT 8/29/2011: New function added to PHP 5.4 to fix this: "Expose session status via new function, session_status" 编辑8/29/2011:添加到PHP 5.4的新功能修复此问题: “通过新函数,session_status公开会话状态”

// as of 8/29/2011
$isSessionActive = (session_status() == PHP_SESSION_ACTIVE);

EDIT 12/5/11: session_status() on the PHP manual. 编辑12/5/11:PHP手册上的session_status()

See edits to the original question; 查看原始问题的编辑; basically, PHP 5.4 and above now has a function called session_status() to solve this problem! 基本上,PHP 5.4及以上版本现在有一个名为session_status()的函数来解决这个问题!

"Expose session status via new function, session_status" (SVN Revision 315745) “通过新函数session_status公开会话状态”(SVN修订版315745)

If you need this functionality in a pre-PHP 5.4 version, see hakre's answer . 如果您在PHP 5.4之前的版本中需要此功能,请参阅hakre的答案

I worked around this by adding a couple wrapper functions around the various session creation/closing/destroying functions. 我通过在各种会话创建/关闭/销毁功能周围添加几个包装函数来解决这个问题。 Basically: 基本上:

function open_session() {
     session_start();
     $_SESSION['is_open'] = TRUE;
}

function close_session() {
   session_write_close();
   $_SESSION['is_open'] = FALSE;
}

function destroy_session() {
   session_destroy();
   $_SESSION['is_open'] = FALSE;
}

function session_is_open() {
   return($_SESSION['is_open']);
}

Hackish, but accomplished what I needed. 哈希,但完成了我所需要的。

I'm running into this as well, and a setting in $_SESSION is not an option for me. 我也遇到了这个问题, $_SESSION的设置对我来说不是一个选项。 For PHP 5.3.8: 对于PHP 5.3.8:

  1. If any session has been started with the request, define('SID') will return FALSE , as well as $_SESSION is unset. 如果已经使用请求启动了任何会话,则define('SID')将返回FALSE ,并且未设置$_SESSION
  2. This is independent whether or not session_id() has been used to set a session id or not. 无论是否已使用session_id()来设置会话ID,这都是独立的。
  3. After the first session_start() , SID is defined and $_SESSION is set to an empty array. 在第一个session_start() ,定义了SID并将$_SESSION设置为空数组。
  4. session_destroy() does unset the session_id() , it's an empty string then. session_destroy()取消设置session_id() ,然后它是一个空字符串。 SID will remain defined (and set to it's previous value which might be an empty string). SID将保持定义(并设置为之前的值,可能是空字符串)。 $_SESSION is left unchanged. $_SESSION保持不变。 It will get reset/populated next time session_start is called. 它将在下次调用session_start重置/填充。

With these states, especially as session_id() can be called in between to set the id for the next session, it's not possible to safely determine the session status with SID , $_SESSION and session_id() . 对于这些状态,尤其是可以在两者之间调用session_id()来设置下一个会话的id,因此无法使用SID$_SESSIONsession_id()安全地确定会话状态。

"Trying" with session_start() (eg with @ ) might not be really helpful, as this will change the session status and changing the contents of $_SESSION (and adding a set-cookie header if the cookie was not part of the request). 使用session_start() (例如使用@ )“尝试”可能不太有用,因为这会改变会话状态并更改$_SESSION的内容(如果cookie不是请求的一部分,则添加set-cookie标头) 。 It was not fitting in my case. 这不适合我的情况。

While I was running tests, I came across the behaviour, that you can not try to change the ini setting of session.serialize_handler when the session is active, not even when you set it to the same value. 当我运行测试时,我遇到了这样的行为,即当会话处于活动状态时,您无法尝试更改session.serialize_handler的ini设置,即使您将其设置为相同的值也是如此。 Same is true for session.use_trans_sid Docs which is more lightweight. session.use_trans_sid 文档也是如此,它更轻量级。 This lead me to the following function: 这引出了以下功能:

/**
 * @return bool
 */
function session_is_active()
{
    $setting = 'session.use_trans_sid';
    $current = ini_get($setting);
    if (FALSE === $current)
    {
        throw new UnexpectedValueException(sprintf('Setting %s does not exists.', $setting));
    }
    $result = @ini_set($setting, $current); 
    return $result !== $current;
}

As far as I can see, the error is checking for active session status only (not disabled), so this should not return a false positive when sessions are disabled. 据我所知,错误是仅检查活动会话状态(未禁用),因此在禁用会话时不应返回误报。

To get this function compatible with PHP 5.2, it needs a little modification: 要使此功能与PHP 5.2兼容,需要稍加修改:

/**
 * @return bool
 */
function session_is_active()
{
    $setting = 'session.use_trans_sid';
    $current = ini_get($setting);
    if (FALSE === $current)
    {
        throw new UnexpectedValueException(sprintf('Setting %s does not exists.', $setting));
    }
    $testate = "mix$current$current";
    $old = @ini_set($setting, $testate);
    $peek = @ini_set($setting, $current);
    $result = $peek === $current || $peek === FALSE;
    return $result;
}

Some sandbox . 一些沙盒

The following code only dumps one session_id() for me, not two 以下代码仅为我转储一个session_id(),而不是两个

session_start();
echo session_id();
session_destroy();
echo session_id();

If you're having difficulties with this still you can try creating a variable to check, that you destroy when you destroy the session. 如果你仍然遇到困难,你可以尝试创建一个变量来检查,当你销毁会话时你会破坏它。

session_start();
$_SESSION['intialized'] = 'This will not print';
$_SESSION = array(); // Unset all variables
session_destroy();
echo $_SESSION['initialized']; // No output

Here's a good drop in replacement that won't break stuff when you move to 5.4: 这是一个很好的替代品,当你转到5.4时不会破坏东西:

if(!function_exists('session_status')){
    function session_active(){
        return defined('SID');   
    }
}else{
    function session_active(){
        return (session_status() == PHP_SESSION_ACTIVE);   
    }        
}

There are multiple places you need to check to verify the session is active: 您需要检查多个位置以验证会话是否处于活动状态:

1- cookie exists and is not expired 2- underlying session storage mechanism (file system or database) has a record matching the cookie. 1- cookie存在且未过期2-基础会话存储机制(文件系统或数据库)具有与cookie匹配的记录。

Ken, if the session is destroyed, the $_SESSION array should not be available...or at the very least, your values should be unset. Ken,如果会话被销毁,$ _SESSION数组应该不可用......或者至少,你的值应该是未设置的。 So, in theory (untested) you should be able to check the array for a value to know if anything is currently set. 因此,理论上(未经测试)您应该能够检查数组中的值以了解当前是否设置了任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM