简体   繁体   English

我可以在每个页面的开头使用session_start()吗,即在我网站的头文件中

[英]Can i use session_start() at the start of every page i.e in the header file of my website

I know if session is not destroyed at the end of some specific task then it will retain its values and could create problems next when you use it. 我知道如果会话在某些特定任务结束时没有被破坏,那么它将保留其值,并且在您使用它时可能会产生问题。 But when you say destroying a session then does it mean ending the session like this 但是,当您说销毁会话时,是否意味着像这样结束会话

session_end()

or it means that you have to unset some specific session which u have set before by doing this. 或这意味着您必须通过执行此操作来取消设置您之前设置的特定会话。

unset($_SESSION['id'] ) unset($_SESSION['id']

etc. And if i start a session at the top of every page and do not set it by $_SESSION['id'] etc then could that create problem for me. 等等。如果我在每个页面的顶部开始一个会话,并且没有通过$ _SESSION ['id']等设置它,那可能会给我带来麻烦。 If yes then why i haven't got proper answer to this anywhere. 如果是,那为什么我在任何地方都没有正确的答案。

Here is how you destroy a session: 这是您破坏会话的方式:

session_destroy();

There is no such thing as session_end() in PHP. PHP中没有session_end()这样的东西。

To empty a specific session variable, you generally do the following: 要清空特定的会话变量,通常需要执行以下操作:

$_SESSION['id'] = '';

If you start a new session on top of every page using session_start it will just make sure the session is setup would it not be active for any reason (destroyed or never started). 如果您使用session_start在每个页面的顶部启动一个新会话,只需确保该会话已建立,无论出于何种原因(已销毁或从未启动),该会话都不会处于活动状态。

It's untrue that there would be no documentation, actually, the web is full with articles and tutorials on session management. 确实没有文档是不正确的,实际上,网络上充斥着有关会话管理的文章和教程。 I myself used the following that helped me a lot to setup my login system of my web app: https://www.owasp.org/index.php/Session_Management . 我本人使用以下对我的Web应用程序的登录系统有用的设置: https : //www.owasp.org/index.php/Session_Management

I also suggest you have a read through all the functions that PHP specifically has to offer starting here: http://www.php.net/manual/en/features.sessions.php . 我还建议您从以下位置开始阅读PHP专门提供的所有功能: http : //www.php.net/manual/en/features.sessions.php

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

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