简体   繁体   English

我需要在PHP中使用session_start()来使用$ _SESSION吗?

[英]Do I need to use session_start() in PHP to use $_SESSION?

I heard my friend say that I don't have to use session_start() to use $_SESSION in PHP? 我听说我的朋友说我不必在PHP中使用session_start()来使用$_SESSION吗? Is that true? 真的吗? If yes, how do I make it work? 如果是,我如何使其工作? If I remove session_start() from my code, I can no longer get $_SESSION to work. 如果我从代码中删除session_start() ,则无法再使$_SESSION正常工作。

Yes it is possible not to have session_start() calls on top of every page necessarily when you want to work with sessions. 是的,当您要使用会话时,不一定必须在每个页面的顶部都调用session_start() Thats the job of session autostart. 那就是会话自动启动的工作。 If you set your session to auto start you can avoid those calls, otherwise you must. 如果将会话设置为自动启动,则可以避免这些呼叫,否则必须这样做。

session.auto_start boolean session.auto_start布尔值

session.auto_start specifies whether the session module starts a session automatically on request startup. session.auto_start指定会话模块是否在请求启动时自动启动会话。 Defaults to 0 (disabled). 默认为0(禁用)。

So if you set session.auto_start to 1 in your php configuration, you wont need to start session manually. 因此,如果您在php配置中将session.auto_start设置为1,则无需手动启动会话。

Manual 手册

PS: It is working fine for your friend and not for you because he/she has enabled session.auto_start and you haven't touched it and by default it is disabled. PS:它对您的朋友很好,对您而言不是很好,因为他/她启用了session.auto_start并且您没有触摸它,并且默认情况下它被禁用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM