php session_start 包含文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6914275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php session_start with include files
提问by Andrea
i learned a lot about session start from my previous question. Now i'm wondering how session locking occurs when files are included in other files. Lets say i have:
我从上一个问题中学到了很多关于会话开始的知识。现在我想知道当文件包含在其他文件中时会话锁定是如何发生的。可以说我有:
page.php
页面.php
include('header.php');
...some html content....
include('sub_page.php');
...more html....
header.php:
头文件.php:
session_start();
..save session vars...
..print web page header...
sub_page.php
子页面.php
session_start();
...use session vars....
..print page content...
When i open page.php, does the session become unlocked as soon as header.php is done? or is it live for the whole page.php life, so sub_page's session is blocked? Is the session_start
in sub_page necessary? Would it be better practice if I session_write_close
every time i'm done with session data? (Though that would mean session_start
ing everytime i'd like to use a session variable).
当我打开 page.php 时,会话是否在 header.php 完成后立即解锁?还是它在整个 page.php 生命中都有效,所以 sub_page 的会话被阻止了?是否session_start
需要 in sub_page?如果我session_write_close
每次都处理完会话数据会更好吗?(尽管这意味着session_start
每次我想使用会话变量时都要这样做)。
采纳答案by sonnb
- You should start session only one time. In your example, just need session_start() at the first line of page.php
- session_start() will generate E_NOTICE if session was previously started. You can use @session_start() to ignore it.
- It also generates E_NOTICE if you use session_start() after you output HTML code.
- 您应该只启动一次会话。在你的例子中,只需要在 page.php 的第一行 session_start()
- session_start() 将生成 E_NOTICE 如果会话先前已启动。您可以使用@session_start() 忽略它。
- 如果在输出 HTML 代码后使用 session_start(),它还会生成 E_NOTICE。
回答by Evan Mulawski
I would recommend creating a session.php
file that you would include once, at the first line of each page. That way, the session is handled in ONE file, in case you need to change validation or session settings (and don't need to worry about your question).
我建议创建一个session.php
文件,在每页的第一行包含一次。这样,会话在一个文件中处理,以防您需要更改验证或会话设置(并且无需担心您的问题)。
回答by James
Due to the answers above talking about errors if session already started, I just wanted to point out you can do:
由于上面讨论了如果会话已经开始的错误的答案,我只想指出您可以这样做:
if (!isset($_SESSION))
{
session_start();
}
Then if the $_SESSION is already started (set) it wont perform the start function.
然后如果 $_SESSION 已经启动(设置)它不会执行启动功能。
Although there's nothing better than a well structured file and folder layout with a good framework setup. Even if just a simple framework structure which separates business logic from presentation.
尽管没有什么比具有良好框架设置的结构良好的文件和文件夹布局更好的了。即使只是一个将业务逻辑与表示分离的简单框架结构。
This way, you'd have something similar to a config folder with initialisation scripts, or at the very least have includefiles in some folder which are included in all pages/scripts.
这样一来,你也有类似的config文件夹与初始化脚本什么的,或者至少是具有包括在其中包含在所有的网页/脚本时的一些文件夹中。
Then you simply have your session_start()
in (depending on your setup) either the very first include file, or in a separate include file and then include that session file when needed in a specific area of the script.
然后,您只需将您的session_start()
输入(取决于您的设置)放在第一个包含文件中,或者在一个单独的包含文件中,然后在需要时在脚本的特定区域中包含该会话文件。
Either way, you then don't need to call it in anyother files, as you know it's simply not required based on your design structure.
无论哪种方式,您都不需要在任何其他文件中调用它,因为您知道根据您的设计结构根本不需要它。
If you do not have a file which is alwaysincluded, then at least use the isset()
check.
如果您没有始终包含的文件,则至少使用isset()
检查。
回答by Brian Gordon
As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.
从 PHP 4.3.3 开始,在会话之前启动后调用 session_start() 将导致级别为 E_NOTICE 的错误。此外,第二个会话开始将被简单地忽略。
回答by noowie
As long as you are not accessing or creating session variables you do not need to worry about session_start(). You only really need to worry about session_start if the script you are running will create session variables, or relies on accessing session variables to function.
只要您不访问或创建会话变量,您就无需担心 session_start()。如果您正在运行的脚本将创建会话变量,或者依赖于访问会话变量来运行,您只需要担心 session_start。
If file1 is not accessing or creating variables for use by other scripts then don't call it. If file2 that is included by file1 is creating or relies on variables in the session then file2 should call session_start(). File2 will be included in the session and will be able to access all session variables, but file1 will not.
如果 file1 没有访问或创建供其他脚本使用的变量,则不要调用它。如果 file1 包含的 file2 正在创建或依赖会话中的变量,则 file2 应调用 session_start()。File2 将包含在会话中并且能够访问所有会话变量,但 file1 不会。
If you call session_start() in file1, then file2 will be able to access all session vars as if it called session_start().
如果您在 file1 中调用 session_start(),那么 file2 将能够访问所有会话变量,就像它调用 session_start() 一样。
Hope this clarifies the situation a bit more.
希望这能进一步澄清情况。
Great tip from James re using isset. This will prevent attempting a pointless session call.
James 重新使用 isset 的重要提示。这将防止尝试无意义的会话调用。
Also check your php.ini file for the session.auto_start var. If this is set to 1 then all files will be run as if they made a session_start() call. Set it to 0 in the php.ini file if you want to control it yourself.
还要检查你的 php.ini 文件中的 session.auto_start 变量。如果将其设置为 1,则所有文件都将像调用 session_start() 一样运行。如果您想自己控制它,请在 php.ini 文件中将其设置为 0。