PHP 会话不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4614671/
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 is not working
提问by Elwhis
I am working with wamp2.0 - PHP 5.3, apache 2.2.11 but my sessions are not storing data.
我正在使用 wamp2.0 - PHP 5.3、apache 2.2.11 但我的会话没有存储数据。
I have a page that accepts a parameter, which (simplified version) I wanna store in a session, so when I come to
我有一个接受参数的页面,它(简化版)我想存储在一个会话中,所以当我来到
http://www.example.com/home.php?sessid=db_session_id
The script looks like:
该脚本如下所示:
session_start();
$sessid = @$_GET['sessid'];
if ($sessid) {
$_SESSION['sessid'] = $sessid;
}
var_dump($_SESSION);
and outputs:
和输出:
array(1) { [0]=> string(13) "db_session_id" }
which is fine, but then, when I go to link(without the sessid parameter) the $_SESSION
array is empty. I've event tried to comment the $_SESSION['sessid'] = $sessid;
line before going to the page without the parameter, but still it didin't work.
这很好,但是,当我去链接(没有 sessid 参数)时,$_SESSION
数组是空的。我在$_SESSION['sessid'] = $sessid;
没有参数的情况下进入页面之前尝试评论该行,但它仍然不起作用。
I've checked the session_id()
output and the session id remains the same.
我检查了session_id()
输出,会话 ID 保持不变。
Session settings from phpinfo()
来自 phpinfo() 的会话设置
Session Support enabled
Registered save handlers files user
Registered serializer handlers php php_binary wddx
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character 5 5
session.hash_function 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path c:/wamp/tmp c:/wamp/tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies On On
session.use_trans_sid 0 0
EDIT:
编辑:
The sessions are deleted by an iframe. I don't know why, but when commented, the sessions work fine. Thing is, the iframe has to stay there (which of course is bad, but I can't do anything about it). Well...do you know about any workarounds to get the sessions working with an iframe?
会话被 iframe 删除。我不知道为什么,但在评论时,会话工作正常。问题是,iframe 必须留在那里(这当然很糟糕,但我对此无能为力)。嗯...你知道让会话使用 iframe 的任何变通方法吗?
采纳答案by JMendoza
A frame can access the session only if it's relative to the same domain. For example:
仅当框架相对于同一域时,它才能访问会话。例如:
<?
$_SESSION["foo"]="foo";
?><html>
<body>
<iframe src ="test.php" width="200" height="200"></iframe>
</body>
</html>
<?
print_r($_SESSION);
?>
Should work outside and inside the iframe. If your still having problems try:
应该在 iframe 的外部和内部工作。如果您仍然遇到问题,请尝试:
<?php session_start();
$sessid = $_GET['sessid'];
if (isset($sessid) && $sessid != "" && $sessid != NULL) {
$_SESSION['sessid'] = $sessid;
}
print_r($_SESSION);?>
回答by Juliano
It is an old question, but I came here with the same problem. As the question was not answered, here it comes what I've found.
这是一个老问题,但我带着同样的问题来到这里。由于问题没有得到解答,因此我发现了它。
I had a similar problem and in my case the problem was I was not calling session_start()
in all the pages where the session was used. I thought I should call it only in the "login" page, but this is not true.
我有一个类似的问题,就我而言,问题是我没有调用session_start()
使用会话的所有页面。我以为我应该只在“登录”页面中调用它,但事实并非如此。
Besides, remember that session_start()
must be called before the <html>
tag.
此外,记住session_start()
必须在<html>
标签之前调用。
回答by JMendoza
You must call the command session_start();
in each page. It worked for me. You must also call this before any output (html, output, even whitespace) is sent to the user.
您必须session_start();
在每个页面中调用该命令。它对我有用。您还必须在将任何输出(html、输出,甚至空格)发送给用户之前调用它。
回答by Martin
Today I spent about four hours with $_SESSION not being saved at the server. It turned out to be a server issue. The system (Windows 2003, IIS 6) used the Windows default temporary folder C:\Windows\Temp\ to store the session. It created it but it was empty.
今天我花了大约四个小时没有将 $_SESSION 保存在服务器上。结果证明是服务器问题。系统(Windows 2003、IIS 6)使用 Windows 默认临时文件夹 C:\Windows\Temp\ 来存储会话。它创造了它,但它是空的。
For some reason it just stopped to work (there was no problem before). My best guess that it reached some limit or the folder simply contained too many files. I sorted it out by setting session.save_path to a different folder.
出于某种原因,它刚刚停止工作(之前没有问题)。我最好的猜测是它达到了某个限制,或者文件夹只是包含太多文件。我通过将 session.save_path 设置为不同的文件夹来整理它。
回答by cwd
If you're getting a similar issue, I find this to be a fairly worthwhile test. Run a few request in the browser and see what you get.
如果您遇到类似的问题,我发现这是一个相当值得的测试。在浏览器中运行一些请求,看看你得到了什么。
<?php
session_start();
$_SESSION['test'][] = time();
header('Content-type: text/plain');
print_r($_SESSION);
To the author: Your last comment mentions that your script is working - good for you. If it breaks then try out the bit of code above and let me know what you get.
致作者:您的最后一条评论提到您的脚本正在运行 - 对您有好处。如果它坏了,请尝试上面的代码,让我知道你得到了什么。
回答by Norm
I can only guess from whats happening there that your session isn't been stored. Is your session.save_path -> c:/wamp/tmp a writable (uncheck read only in properties) folder? Is apache being run as an administrator or some level with sufficient permissions to write to that folder in services in the administrator tools in control panel?
我只能从那里发生的事情猜测您的会话没有被存储。您的 session.save_path -> c:/wamp/tmp 是可写的(在属性中取消选中只读)文件夹吗?apache 是以管理员身份运行还是某个级别具有足够的权限来写入控制面板中管理员工具中的服务中的该文件夹?
Actually, check this: PHP session vars not being maintained. Essentially, run session_id() on that page twice and compare the session ids. If they are different then your browser is not accepting session cookies.
实际上,请检查:未维护 PHP 会话变量。本质上,在该页面上运行 session_id() 两次并比较会话 ID。如果它们不同,则您的浏览器不接受会话 cookie。
EDIT: Didn't realize you already did that. ^
编辑:没有意识到你已经这样做了。^
This might help. Link<--- Look at the second suggestion/answer with the debugging code for session.
这可能会有所帮助。链接<--- 使用会话的调试代码查看第二个建议/答案。
回答by funkydata
http://php.net/manual/en/function.session-start.php
http://php.net/manual/en/function.session-start.php
You have to put something inside the $_SESSION global array for it to register anything, otherwise, it's empty...
你必须在 $_SESSION 全局数组中放一些东西才能注册任何东西,否则,它是空的......
Since you are testing the existence of sessid to add it to $_SESSION, $_SESSION remains empty.
由于您正在测试 sessid 的存在以将其添加到 $_SESSION,因此 $_SESSION 保持为空。
Try something like:
尝试类似:
session_start();
$_SESSION['sessid'] = 123456789;
var_dump($_SESSION);
回答by funkydata
...
...
It could be because you are not using a domain per se? In your question, you use example.com, however you are developing on your local machine. I assume you are using localhost.
可能是因为您本身没有使用域?在您的问题中,您使用 example.com,但是您是在本地机器上开发的。我假设您使用的是本地主机。
You should know that one cannot set cookies on localhost, see for example: Cookies on localhost with explicit domain
您应该知道不能在 localhost 上设置 cookie,请参见示例:带有显式域的 localhost 上的 Cookies
Have you tried your code on another host?
您是否在另一台主机上尝试过您的代码?
http://blog.perplexedlabs.com/2008/12/21/php-sessions-on-localhost/
http://blog.perplexedlabs.com/2008/12/21/php-sessions-on-localhost/
回答by jeroen
I don′t know if it is the cause of your problem (the error-suppressing @
seems a bit out-of-place here...), but I would simplify a bit.
我不知道这是否是您的问题的原因(错误抑制@
在这里似乎有点不合适......),但我会简化一点。
So instead of:
所以而不是:
$sessid = @$_GET['sessid'];
if ($sessid) {
$_SESSION['sessid'] = $sessid;
}
I would use:
我会用:
if (isset($_GET['sessid']))
{
$_SESSION['sessid'] = $_GET['sessid'];
}
By the way, I would also do some input-checking on $_GET['sessid']
to make sure it only contains the characters allowed.
顺便说一下,我还会做一些输入检查$_GET['sessid']
以确保它只包含允许的字符。