已经启动的 PHP 会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10648984/
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
PHP sessions that have already been started
提问by user1400702
In my PHP code, if a session has already started, and I try to start a new one, I get the following Notice:
在我的 PHP 代码中,如果一个会话已经开始,而我尝试开始一个新的会话,我会收到以下通知:
Notice: A session had already been started - ignoring session_start()
注意:会话已经开始 - 忽略 session_start()
How can I avoid this?
我怎样才能避免这种情况?
回答by Valeriy Gorbatikov
Try
尝试
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
回答by Juan Cortés
If you want a new one, then do session_destroy()before starting it.
To check if its set before starting it, call session_status():
如果你想要一个新的,那么session_destroy()在开始之前先做。要在启动之前检查它是否已设置,请调用session_status():
$status = session_status();
if($status == PHP_SESSION_NONE){
//There is no active session
session_start();
}else
if($status == PHP_SESSION_DISABLED){
//Sessions are not available
}else
if($status == PHP_SESSION_ACTIVE){
//Destroy current and start new one
session_destroy();
session_start();
}
I would avoid checking the global $_SESSIONinstead ofI am calling the session_status()method since PHP implemented this function explicitly to:
我会避免检查全局$_SESSION,而不是的,我呼吁的session_status()方法,因为PHP明确实现此功能:
Expose session status via new function, session_status This is for (PHP >=5.4.0)
通过新函数 session_status 公开会话状态这适用于 (PHP >=5.4.0)
回答by Code Spy
Only if you want to destroy previous session :
仅当您想销毁上一个会话时:
<?php
if(!isset($_SESSION))
{
session_start();
}
else
{
session_destroy();
session_start();
}
?>
or you can use
或者你可以使用
unset($_SESSION['variable_session _data'])
to destroy a particular session variable.
销毁特定的会话变量。
回答by Spudley
Yes, you can detect if the session is already running by checking isset($_SESSION). However the best answer is simply not to call session_start()more than once.
是的,您可以通过检查来检测会话是否已经在运行isset($_SESSION)。然而,最好的答案是不要session_start()多次调用。
It should be called very early in your script, possibly even the first line, and then not called again.
它应该在您的脚本中很早就被调用,甚至可能是第一行,然后不再调用。
If you have it in more than one place in your code then you're asking to get this kind of bug. Cut it down so it's only in one place and can only be called once.
如果您在代码中不止一个地方拥有它,那么您就会要求获得这种错误。把它剪下来,让它只在一个地方,只能被调用一次。
回答by MikeCruz13
You must of already called the session start maybe being called again through an include?
您必须已经调用会话开始,可能会通过包含再次调用?
if( ! $_SESSION)
{
session_start();
}
回答by TrophyGeek
I encountered this issue while trying to fix $_SESSION's blocking behavior.
我在尝试修复 $_SESSION 的阻塞行为时遇到了这个问题。
http://konrness.com/php5/how-to-prevent-blocking-php-requests/
http://konrness.com/php5/how-to-prevent-blocking-php-requests/
The session file remains locked until the script completes or the session is manually closed.
会话文件保持锁定状态,直到脚本完成或手动关闭会话。
So, by default, a page should open a session in read-only mode. But once it's open in read-only, it has to be closed-and-reopened in to get it into write mode.
因此,默认情况下,页面应以只读模式打开会话。但是一旦它以只读方式打开,就必须关闭并重新打开以使其进入写入模式。
const SESSION_DEFAULT_COOKIE_LIFETIME = 86400;
/**
* Open _SESSION read-only
*/
function OpenSessionReadOnly() {
session_start([
'cookie_lifetime' => SESSION_DEFAULT_COOKIE_LIFETIME,
'read_and_close' => true, // READ ACCESS FAST
]);
// $_SESSION is now defined. Call WriteSessionValues() to write out values
}
/**
* _SESSION is read-only by default. Call this function to save a new value
* call this function like `WriteSessionValues(["username"=>$login_user]);`
* to set $_SESSION["username"]
*
* @param array $values_assoc_array
*/
function WriteSessionValues($values_assoc_array) {
// this is required to close the read-only session and
// not get a warning on the next line.
session_abort();
// now open the session with write access
session_start([ 'cookie_lifetime' => SESSION_DEFAULT_COOKIE_LIFETIME ]);
foreach ($values_assoc_array as $key => $value) {
$_SESSION[ $key ] = $value;
}
session_write_close(); // Write session data and end session
OpenSessionReadOnly(); // now reopen the session in read-only mode.
}
OpenSessionReadOnly(); // start the session for this page
Then when you go to write some value:
然后当你去写一些值时:
WriteSessionValues(["username"=>$login_user]);
The function takes an array of key=>value pairs to make it even more efficient.
该函数采用键=> 值对的数组,使其更加高效。
回答by Jos3
It would be more efficient:
效率会更高:
@session_start();
Avoiding error handler in the screen
避免屏幕中的错误处理程序
Best,
最好的事物,
回答by Crabthug
session_status() === PHP_SESSION_ACTIVE ?: session_start();
Closed Game
封闭式游戏
回答by Odinn
<?php
if ( session_id() != "" ) {
session_start();
}
Basically, you need to check if a session was started before creating another one; ... more reading.
基本上,您需要在创建另一个会话之前检查会话是否已启动;... 更多阅读。
On the other hand, you chose to destroy an existing session before creating another one using session_destroy().
另一方面,您选择在使用session_destroy().
回答by user2521037
try this
尝试这个
if(!isset($_SESSION)){
session_start();
}
I suggest you to use ob_start(); before starting any sesson varriable, this should order you browser buffer.
我建议你使用 ob_start(); 在开始任何 sesson 变量之前,这应该为您的浏览器缓冲区排序。
edit: Added an extra ) after $_SESSION
编辑:在 $_SESSION 之后添加了一个额外的 )

