php 仅当会话不存在时才加载 session_start()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10093264/
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-24 21:21:36  来源:igfitidea点击:

Load session_start() only if session does not exist?

phpsession

提问by acctman

is there a short code trick to check if a session has started and if not then load one? Currently I receive an error "session already started..." if I put in a session start regardless of checking.

是否有一个简短的代码技巧来检查会话是否已开始,如果没有,则加载一个?目前我收到一个错误“会话已经开始......”,如果我不管检查而输入会话开始。

回答by Rooster

issetis generally the proper way to check if predefined variables are currently defined:

isset通常是检查当前是否定义了预定义变量的正确方法:

If you are using a version of php prior to 5.4,

如果您使用的是 5.4 之前的 php 版本

you can usually get away with and its tempting just doing the code below, but be aware that if for some reason sessions are disabled, session_start() will still be called which could end up leading to errors and needless debugging since the $_SESSION array isn't allowed to be created.

你通常可以逃脱它的诱惑,只需执行下面的代码,但请注意,如果由于某种原因会话被禁用, session_start() 仍将被调用,这可能最终导致错误和不必要的调试,因为 $_SESSION 数组是'不允许被创建。

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

if you want to account for checking to see if sessions are disabled for some reason, you can supress the error message, set a test session variable, and then verify it was set. If its not, you can code in a reaction for disabled sessions. You'll want to do all this at or near the start of your script. So as an example(works differently depending on error handling setting):

如果您想检查会话是否由于某种原因被禁用,您可以抑制错误消息,设置测试会话变量,然后验证它是否已设置。如果不是,您可以对禁用的会话进行编码。您需要在脚本开始时或附近完成所有这些操作。所以作为一个例子(根据错误处理设置的不同,工作方式不同):

if(!isset($_SESSION)){session_start();}
$_SESSION['valid'] = 'valid';
if($_SESSION['valid'] != 'valid')
{
   //handle disabled sessions
}

However, if you are using php version 5.4 or greater,

但是,如果您使用的是 php 5.4 或更高版本

You can use the session_status()function, a better option as it accounts for sessions being disabled and checks if a session already exists.

您可以使用session_status()函数,这是一个更好的选择,因为它会考虑禁用会话并检查会话是否已存在。

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

NOTE that PHP_SESSION_NONEis a constant set by PHP and therefore does not need to be wrapped in quotes. It evaluates to integer 1, but as its a constant that was specifically set to eliminate the need for magic numbers, best to test against the constant.

注意PHP_SESSION_NONE是由 PHP 设置的常量,因此不需要用引号括起来。它的计算结果为整数 1,但由于它是一个专门为消除幻数需要而设置的常量,最好针对该常量进行测试。

回答by dqhendricks

Many people simply use @session_start()to suppress the warning that the session has already been started. The alternative is usually:

许多人只是@session_start()用来抑制会话已经开始的警告。替代方案通常是:

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

回答by mcems7

try this condition

试试这个条件

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

If you only want to avoid the error, use @ before the function example:

如果只想避免错误,请在函数示例前使用@:

@session_start();

回答by Mahesh Cheliya

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

回答by recurse

Checking for existing session before starting new:

在开始新会话之前检查现有会话:

   // # if phpversion() is >= 5.4
  if(phpversion() >= 5.4) {

    // # check session_status() function for value 'PHP_SESSION_NONE'
    if(session_status() === PHP_SESSION_NONE) {
      session_start();
    }

  } else { // # maintain backwards compat. with PHP versions older than 5.4

      // # if $_SESSION object if NOT set, start a new session!
      if(!isset($_SESSION)) {
        session_start();
      }
  }