问题
I'm having problems with $_SESSION
superglobal on AJAX request.
session_start()
function is called before any session coding. Session ID is also the same in the calling code and the AJAX response code (tested by echoing session_id()
in both scripts). AJAX PHP file is on the same domain. Everything should work as defined by standards, but when I do print_r($_SESSION)
in the called AJAX script file I get Arrray( )
output.
I've hit the brick wall... I don't know why is this not working...
Checked both in Chrome and Firefox.
Any ideas?
UPDATE:
The problem is with $.ajax(...)
request! When I do AJAX request it knows right session ID, and the session_start()
function returns TRUE
(successfully continued session) but then it resets my $_SESSSION
superglobal! It empties it out... I don't know why yet...
Code:
index.php:
<?php
session_start();
$_SESSION['Test']='O.K.';
echo("SESSION_ID: " . session_id());
echo("SESSION_SIZE:" . sizeof($_SESSION));
?>
... Standard HTML stuff and jQuery include ...
<script>
$.ajax(
{
type: "POST",
url: "AJAXTest.php",
data: null,
success: function(sData) { alert(sData); }
});
</script>
AJAXTest.php:
<?php
session_start();
echo("SESSION_ID: " . session_id());
echo("SESSION_SIZE:" . sizeof($_SESSION));
?>
index.php output:
SESSION_ID: xxxxxxxxxxxxxxxxxxxxxxx
SESSION_SIZE: 1
Alert output:
SESSION_ID: xxxxxxxxxxxxxxxxxxxxxxx (right session id)
SESSION_SIZE: 0
And after the AJAX call $_SESSION is empty. Across all other scripts with the same session... I'm baffled...
回答1:
it might be because if u didn't put any value in $_SESSION
. it will show you array() on doing print_r($_SESSION)
try setting up a value $_SESSION['user']='frankie'
then do print_r($_SESSION);
session_id() is never shown in $_SESSION array.
回答2:
It might be a problem not with AJAX, but with the session itself. Now you are just testing the $_SESSION array and session id, but not the session storage itself. Try to see if session state keeps the same in several non-AJAX requests. For example, use this:
$_SESSION['Test' . time()]='O.K.';
Instead of this:
$_SESSION['Test']='O.K.';
When refreshing the page, your SESSION_SIZE count should increase. If it does not increase, maybe session storage parameters in php.ini are incorrect? For example, problems while writing sessions to files or issues with memcache if you use that for sessions.
Also be sure that no other requests are made between page and ajax call - maybe some called script resets your $_SESSION array?
回答3:
The problem was in my custom php.ini file... It obviously screwed up some session important settings (even they were not defined -> changed).
Result was that every call to session_start() would reset $_SESSION superglobal and empty it, but leave the same session ID what confused me and threw in the wrong direction. Until I've stripped down to the bone everything it was clear that error was not in my code.
Thanks everyone who took interest.
来源:https://stackoverflow.com/questions/10967156/empty-session-superglobal-on-ajax-request-but-session-id-is-preserved