I have a third party script and was wondering how I can check with PHP
if session_start()
has been declared before doing something?
我有一个第三方脚本,并想知道如果在做某事之前已经声明了session_start(),我怎么能用PHP检查?
something like if(isset($_session_start())) { //do something }
10 个解决方案
#1
37
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.
检查将允许您保持当前会话处于活动状态,即使它是一个环回提交应用程序,如果数据输入不正确,或者在继续执行下一个程序之前在程序中有其他检查和平衡,则计划重新使用该表单。
#2
25
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中阅读更多相关信息
#3
13
I suppose you could use the session_id
function :
我想你可以使用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 $_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.
或者也许测试是否设置$ _SESSION,使用isset,可能会成功,因为它不应该在没有启动会话时设置 - 你只需要在没有首先启动会话的情况下为$ _SESSION分配任何内容。
#4
7
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();
}
#5
2
if(isset($_SESSION)) {
// do something
}
#6
1
function session_started(){ return !!session_id(); }
#7
0
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();
}
#8
0
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;
}
}
#9
0
best way work for me! if(session_status() != 1) session_start();
最好的方式为我工作! if(session_status()!= 1)session_start();
#10
-1
if(defined('SID'))
// do
#1
37
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.
检查将允许您保持当前会话处于活动状态,即使它是一个环回提交应用程序,如果数据输入不正确,或者在继续执行下一个程序之前在程序中有其他检查和平衡,则计划重新使用该表单。
#2
25
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中阅读更多相关信息
#3
13
I suppose you could use the session_id
function :
我想你可以使用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 $_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.
或者也许测试是否设置$ _SESSION,使用isset,可能会成功,因为它不应该在没有启动会话时设置 - 你只需要在没有首先启动会话的情况下为$ _SESSION分配任何内容。
#4
7
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();
}
#5
2
if(isset($_SESSION)) {
// do something
}
#6
1
function session_started(){ return !!session_id(); }
#7
0
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();
}
#8
0
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;
}
}
#9
0
best way work for me! if(session_status() != 1) session_start();
最好的方式为我工作! if(session_status()!= 1)session_start();
#10
-1
if(defined('SID'))
// do