检查 $_SESSION 变量的顺序是什么?

发布于 2024-12-25 19:36:04 字数 188 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦情居士 2025-01-01 19:36:04

正如您看到的所有答案,如果不使用 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 the session_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

铁轨上的流浪者 2025-01-01 19:36:04

在使用会话之前必须调用 session_start()

It is a must that you call session_start() before using sessions.

画离情绘悲伤 2025-01-01 19:36:04

在使用会话之前,您应该首先调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文