问题
I am working with wamp2.0 - PHP 5.3, apache 2.2.11 but my sessions are not storing data.
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.
I've checked the session_id()
output and the session id remains the same.
Session settings from 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?
回答1:
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:
<?php session_start();
$sessid = $_GET['sessid'];
if (isset($sessid) && $sessid != "" && $sessid != NULL) {
$_SESSION['sessid'] = $sessid;
}
print_r($_SESSION);?>
回答2:
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.
Besides, remember that session_start()
must be called before the <html>
tag.
回答3:
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.
回答4:
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.
回答5:
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.
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.
回答6:
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...
Since you are testing the existence of sessid to add it to $_SESSION, $_SESSION remains empty.
Try something like:
session_start();
$_SESSION['sessid'] = 123456789;
var_dump($_SESSION);
回答7:
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?
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.
EDIT: Didn't realize you already did that. ^
This might help. Link <--- Look at the second suggestion/answer with the debugging code for session.
回答8:
...
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.
You should know that one cannot set cookies on localhost, see for example: Cookies on localhost with explicit domain
Have you tried your code on another host?
http://blog.perplexedlabs.com/2008/12/21/php-sessions-on-localhost/
回答9:
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.
来源:https://stackoverflow.com/questions/4614671/php-session-is-not-working