php 如何检查 session_start 是否已输入?

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

How do i check if session_start has been entered?

php

提问by iaddesign

I have a third party script and was wondering how I can check with PHPif session_start()has been declared before doing something?

我有一个第三方脚本,想知道如何在做某事之前检查PHP是否session_start()已声明?

something like if(isset($_session_start())) { //do something } 

回答by Maarek

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

The check will allow you to keep your current session active even if it's a loop back submission application where you plan to reuse the form if data is typed incorrectly or have additional checks and balances within your program before proceeding to the next program.

检查将允许您保持当前会话处于活动状态,即使它是一个环回提交应用程序,如果数据输入不正确或在继续下一个程序之前在程序中进行额外的检查和平衡,您计划重用表单。

回答by PhoneixS

As in PHP >= 5.4.0, you have function session_status() that tell you if it have been initialize or not or if it's disabled.

就像在 PHP >= 5.4.0 中一样,你有函数 session_status() 来告诉你它是否已经被初始化或者它是否被禁用。

For example you can do:

例如你可以这样做:

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

You can read more about it in http://www.php.net/manual/en/function.session-status.php

您可以在http://www.php.net/manual/en/function.session-status.php 中阅读更多相关信息

回答by Pascal MARTIN

I suppose you could use the session_idfunction :

我想你可以使用这个session_id功能:

session_id()returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).

session_id()如果没有当前会话(不存在当前会话 ID),则返回当前会话的会话 ID 或空字符串 ("")。

Or maybe testing whether $_SESSIONis set or not, with isset, might do the trick, as it should not be set when no session has been started -- you just have to hope that nothing assigns anything to $_SESSIONwithout starting the session first.

或者也许测试是否$_SESSION设置,使用isset, 可能会起作用,因为在没有启动会话时不应该设置它——你只需要希望$_SESSION没有先启动会话就没有分配任何东西。

回答by krishnaTORQUE

for php >= 5.4.0

对于 php >= 5.4.0

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

for php < 5.4.0

对于 php < 5.4.0

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

回答by Rob

if(isset($_SESSION)) {
    // do something
}

回答by Rob

function session_started(){ return !!session_id(); }

回答by 20AMax

If session already started, Error: "Fatal error: Can't use function return value in write context"

如果会话已经开始,错误:“致命错误:不能在写上下文中使用函数返回值”

Try this:

尝试这个:

$session = session_id();
if(empty($session)){ 
    session_start();
}

回答by Yahya

I just wrote a simple function for it.

我只是为它写了一个简单的函数。

  function isSessionStart ()
  {
    if (version_compare(phpversion(), '5.4.0', '<')) {
      if(session_id() == '') {
        return false;
      }
      return true;
    }
    else {
      if (session_status() == PHP_SESSION_NONE) {
        return false;
      }
      return true;
    }
  }

回答by Abolfazl Miadian

best way work for me! if(session_status() != 1) session_start();

对我来说最好的工作方式!if(session_status() != 1) session_start();

回答by Romka


    if(defined('SID'))
        // do