How do i check if session

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

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

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

该检查将允许您保持当前会话处于活动状态,即使它是一个循环提交应用程序,如果您计划在数据输入不正确时重新使用表单,或者在继续执行下一个程序之前在程序中进行了其他检查和平衡,那么该循环提交应用程序仍然有效。


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.

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


I suppose you could use the session_id function :

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).

Or maybe testing whether $_SESSION is 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 $_SESSION without starting the session first.

链接地址: http://www.djcxy.com/p/57738.html

上一篇: 如何检查用户是否登录

下一篇: 我如何检查会话