如何在 PHP 中使用 Session 和 Include 语句?
我有许多文件,其中一个包含多个,如下所示,
File1
包含在 File2
中,然后 File2
位于 File3< /code>
现在我想从 File1 声明一个会话变量 site_user_conutry
,然后我检查 File2,如果会话变量中没有任何值,那么只有我包含
我添加的 File1.php session_start();
在每个[年龄但仍然不起作用?
请告诉我如何在上述情况下使会话正常工作。
I am having many files with multple includes one into another like below,
File1
is included in File2
and then File2
is under File3
Now I want to declare a session variable site_user_conutry
from File1 and then I am checking on File2 that if there is any no value in the session variable then Only I am including File1.php
I have added session_start();
in each [age but still its not working ??
Please tell me how can I make session work in the above condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在文件2内
Within File2
在第一行代码中只执行一次 session_start 。
文档说:
从版本 4.4.3 开始,在会话已经启动时调用 session_start() 将导致 E_NOTICE 级别的错误。 此外,第二个会话开始将被简单地忽略。
如果 file1 包含在 file2 中,则 file2 中的代码将在 file1 之前完成。 因此,如果您在 file1 中声明某些内容,则可能不会在 file2 中看到(取决于代码...),无论如何,这不是 php 编程的现代方式。 (参见 oo 和 __autoload)
Do session_start in first lines of code just once.
Documentation says:
As from version 4.4.3, calling session_start() while the session has already been started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.
If file1 is included in file2 that code from file2 is done before file1. So if you declare something in file1 may not be seen in file2 (depends on code...), anyway it is not modern way pf php programming. (see oo and __autoload)
您如何设置会话变量? 您应该尝试像这样设置它:
您是否将 session_start() 放在文档的最顶部,或者至少在输出任何信息之前?
How are you setting the session variable? You should try setting it like so:
Have you placed session_start() at the very top of the document, or at least before you output any information?
假设您包含具有代码执行而不是函数的文件。
第一次迭代时,包含 File1 并设置会话变量的代码必须在 File2 上的代码之前执行。
在接下来的迭代中,会话将为您保存这些值。
要设置会话变量,请使用 $_SESSION 全局数组。
Assuming that you are including files that have code execution, not functions.
The code that includes File1 and sets the session variables must be executed before the code on File2 on the first iteraction.
In the next iteractions the session will hold the values for you.
To set an session variable use the $_SESSION global array.