检查 $_SESSION 变量的顺序是什么?
在 php 中,我想检查用户是否已登录。通常是这样完成的
if (isset($_SESSION['user_name'])
问题是。在这个检查之前是否需要启动session(在代码中写入session_start()
)?或者是否可以在不启动会话的情况下检查会话变量?
In php I want to check if the user is logged in. Usualy it is done like this
if (isset($_SESSION['user_name'])
The question is. Is it necessary to start session ( write session_start()
in the code ) before this check? Or is it possible to check session var without starting a session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您看到的所有答案,如果不使用
session_start()
就无法获取会话变量
,那么必须使用
session_start()
> 在检查任何会话变量之前,另外,如果您使用
if(isset($_SESSION['username']))
,它仅在您之前启动会话时才有效As you see all the answer there is no way to get the
session variables
without using thesession_start()
then it is must to use
session_start()
before checking any session variable,also if you using
if(isset($_SESSION['username']))
it will only work when you start the session before在使用会话之前必须调用
session_start()
。It is a must that you call
session_start()
before using sessions.在使用会话之前,您应该首先调用
session_start()
(在将任何内容发送到浏览器之前,甚至是空格)。我确信手册上也是这么说的。您在使用会话的所有页面上都需要它。
You should call
session_start()
first thing (before you send any content to the browser, even whitespace) and before you use the session. I'm sure that's what the manual says too.You need it on all pages that use the session.
您需要将 session_start() 或 session.auto_start 调用为 true。
http://www.php.net/manual /en/session.configuration.php#ini.session.auto-start
http://php.net/manual/en/function.session-start.php
You need to call session_start() OR session.auto_start to true.
http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start
http://php.net/manual/en/function.session-start.php