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...