已经开始的PHP会话

在我的PHP代码中,如果一个会话已经开始,而我尝试启动一个新会话,则会收到以下通知:


注意:会话已经开始-忽略session_start()


如何避免这种情况?


红颜莎娜
浏览 264回答 3
3回答

扬帆大鱼

尝试<?php&nbsp; &nbsp; if(!isset($_SESSION))&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp;&nbsp; &nbsp; }&nbsp;?>

莫回无

如果您想要一个新的,请session_destroy()在开始之前进行操作。要在启动之前检查其设置,请致电session_status():$status = session_status();if($status == PHP_SESSION_NONE){&nbsp; &nbsp; //There is no active session&nbsp; &nbsp; session_start();}elseif($status == PHP_SESSION_DISABLED){&nbsp; &nbsp; //Sessions are not available}elseif($status == PHP_SESSION_ACTIVE){&nbsp; &nbsp; //Destroy current and start new one&nbsp; &nbsp; session_destroy();&nbsp; &nbsp; session_start();}我会避免检查全局$_SESSION,而不是的 ,我呼吁的session_status()方法,因为PHP明确实现此功能:通过新函数session_status公开会话状态这是针对(PHP> = 5.4.0)

湖上湖

仅当您想破坏上一个会话时:<?php&nbsp; &nbsp; if(!isset($_SESSION))&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; session_destroy();&nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp;&nbsp; &nbsp; }?>或者你可以使用unset($_SESSION['variable_session _data'])销毁特定的会话变量。
打开App,查看更多内容
随时随地看视频慕课网APP