检查 PHP 会话是否已经启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6249707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:47:24  来源:igfitidea点击:

Check if PHP session has already started

phpsessionsession-variables

提问by Logan

I have a PHP file that is sometimes called from a page that has started a session and sometimes from a page that doesn't have session started. Therefore when I have session_start()on this script I sometimes get the error message for "session already started". For that I've put these lines:

我有一个 PHP 文件,有时从已启动会话的页面调用,有时从未启动会话的页面调用。因此,当我使用session_start()此脚本时,有时会收到“会话已启动”的错误消息。为此,我放置了以下几行:

if(!isset($_COOKIE["PHPSESSID"]))
{
  session_start();
}

but this time I got this warning message:

但这次我收到了这条警告信息:

Notice: Undefined variable: _SESSION

注意:未定义变量:_SESSION

Is there a better way to check if session has already started?

有没有更好的方法来检查会话是否已经开始?

If I use @session_startwill it make things work properly and just shut up the warnings?

如果我使用@session_start它会使事情正常工作并关闭警告吗?

回答by lovelyramos

Recommended way for versions of PHP >= 5.4.0 , PHP 7

PHP >= 5.4.0 , PHP 7版本的推荐方式

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

Reference: http://www.php.net/manual/en/function.session-status.php

参考:http: //www.php.net/manual/en/function.session-status.php

For versions of PHP < 5.4.0

对于PHP < 5.4.0 的版本

if(session_id() == '') {
    session_start();
}

回答by Alex

For versions of PHP prior to PHP 5.4.0:

对于 PHP 5.4.0 之前的 PHP 版本:

if(session_id() == '') {
    // session isn't started
}

Though, IMHO, you should really think about refactoring your session management code if you don't know whether or not a session is started...

不过,恕我直言,如果您不知道会话是否已启动,您应该真正考虑重构会话管理代码......

That said, my opinion is subjective, and there are situations (examples of which are described in the comments below) where it may not be possible to know if the session is started.

也就是说,我的意见是主观的,在某些情况下(在下面的评论中描述了示例)可能无法知道会话是否开始。

回答by Benjamin

PHP 5.4 introduced session_status(), which is more reliable than relying on session_id().

PHP 5.4 引入了session_status(),它比依赖session_id().

Consider the following snippet:

考虑以下片段:

session_id('test');
var_export(session_id() != ''); // true, but session is still not started!
var_export(session_status() == PHP_SESSION_ACTIVE); // false

So, to check whether a session is started, the recommended way in PHP 5.4 is now:

因此,要检查会话是否已启动,现在 PHP 5.4 中推荐的方法是:

session_status() == PHP_SESSION_ACTIVE

回答by miyuru

you can do this, and it's really easy.

你可以做到这一点,这真的很容易。

if (!isset($_SESSION)) session_start();

Hope it helps :)

希望能帮助到你 :)

回答by k.bon

if (version_compare(phpversion(), '5.4.0', '<')) {
     if(session_id() == '') {
        session_start();
     }
 }
 else
 {
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
 }

回答by R Y

Prior to PHP 5.4 there is no reliable way of knowing other than setting a global flag.

在 PHP 5.4 之前,除了设置全局标志外,没有其他可靠的方法可以获知。

Consider:

考虑:

var_dump($_SESSION); // null
session_start();
var_dump($_SESSION); // array
session_destroy();
var_dump($_SESSION); // array, but session isn't active.

Or:

或者:

session_id(); // returns empty string
session_start();
session_id(); // returns session hash
session_destroy();
session_id(); // returns empty string, ok, but then
session_id('foo'); // tell php the session id to use
session_id(); // returns 'foo', but no session is active.

So, prior to PHP 5.4 you should set a global boolean.

因此,在 PHP 5.4 之前,您应该设置一个全局布尔值。

回答by supersuphot

For all php version

对于所有 php 版本

if ((function_exists('session_status') 
  && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) {
  session_start();
}

回答by Elshan

Check this :

检查这个:

<?php
/**
* @return bool
*/
function is_session_started()
{
    if ( php_sapi_name() !== 'cli' ) {
        if ( version_compare(phpversion(), '5.4.0', '>=') ) {
            return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return session_id() === '' ? FALSE : TRUE;
        }
    }
    return FALSE;
}

// Example
if ( is_session_started() === FALSE ) session_start();
?>

Source http://php.net

来源http://php.net

回答by Wesley van Opdorp

Use session_id(), it returns an empty string if not set. It's more reliable than checking the $_COOKIE.

使用session_id(),如果未设置,则返回空字符串。它比检查$_COOKIE.

if (strlen(session_id()) < 1) {
    session_start();
}

回答by Mahesh Cheliya

if (session_id() === "") { session_start(); }

hope it helps !

希望能帮助到你 !