PHP session_start() 函数:为什么每次使用与 PHP 会话相关的内容时都需要它
为了从我的网站注销用户,我将页面重定向到使用 session_destroy() 函数的 logout.php
。即使在那里,如果没有 session_start()
函数,注销功能也无法工作。通过在 session_destroy()
函数之前添加 session_start() 函数,我能够成功注销用户。
为什么我需要在每次执行与会话相关的操作的每个页面中使用 session_start()
函数?
For logging out a user from my website, I am redirecting the page to logout.php
where I am using session_destroy() function. Even there also, logout functionality is not working without session_start()
function. By adding session_start() function before session_destroy()
function, I am able to logout the user successfully.
Why do I need to use session_start()
function everytime and in every page where I am doing something related to sessions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
session_destroy() 销毁活动会话。如果不初始化会话,则不会有任何内容被销毁。
session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.
所以 PHP 知道要销毁哪个会话。
session_start()
查看会话 cookie 或 ID 是否存在。只有掌握了这些信息,你才能摧毁它。So PHP knows which session to destroy.
session_start()
looks whether a session cookie or ID is present. Only with that information can you destroy it.在默认配置中,PHP 会话在硬盘上运行。 PHP 要求您明确告诉它何时需要此支持,以避免不必要的磁盘 IO。
session_start()
还告诉 PHP 查找用户的会话是否存在。In the default configuration, PHP Sessions operate off of the hard disk. PHP asks you to explicitly tell it when you need this support to avoid unnecessary disk IO.
session_start()
also tells PHP to find out if the user's session exists.根据 http://php.net/manual/en/function.session-start .php
本质上,通过调用
session_start()
,PHP 读取标头并将会话 ID 交叉引用到系统上的内容(文件系统/数据库/等),然后填充$_SESSION
表示与该特定用户相关。这反过来又允许您调用session_destroy()
因为它知道实际销毁哪个会话。as per http://php.net/manual/en/function.session-start.php
Essentially by calling
session_start()
, PHP reads the header and cross references that session ID to what is on your system(file system/database/etc), which can then populate the$_SESSION
that is relavent to that specific user. Which in turn allows you to callsession_destroy()
because it knows what session to actually destroy.将 session_start() 视为告诉 php 引擎您想要使用会话的方式。
而且,据我了解,始终将其作为 php 页面中的第一行。
consider session_start() as your way of telling the php engine.... that you want to work with sessions.
and, as i understand it, always make that to be the first line ever in php page.
我对 session_start(); 的用法感到困惑每次我使用会话变量时,我都会调用 session_start。准确地说,我有 session_start();每个页面上多次(甚至没有调用 session_destroy())。例如,
这给我带来了性能问题。所以我最终在页面的最顶部调用了一次
session_start();
,一切似乎都工作正常。I was confused with the usage of session_start(); and every time I was using a session variable, I was calling session_start. Precisely, I had session_start(); more than once on each page (without even calling session_destroy()). For example,
This was creating a performance issue for me. So I ended up calling
session_start();
just once at the very top of the page and everything seems to be working fine.您必须在您希望会话在其中工作的每个文件中调用 session_start 一次(且仅一次)。
允许您只调用一次的常见方法是使用一个调度程序文件作为您的index.php;在这里调用 session_start 并让此页面根据 url 的 $_GET 包含其他页面。
You have to call session_start once (and only once) in every file you want sessions to work in.
A common approach allowing you to only call it once is to have a dispatcher file as your index.php; call session_start in here and have this page include others based on the url's $_GET.