4

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

hakre
  • 193,403
  • 52
  • 435
  • 836
StjepanV
  • 167
  • 2
  • 14
  • do yo have any code before `session_start()` ? – dev-null-dweller Jun 10 '12 at 08:03
  • Do you have cookies enabled in your browser? – somnath Jun 10 '12 at 08:10
  • check it with var_dump() – sohaan Jun 10 '12 at 08:26
  • You may have ommited some important code. – Gabriel Santos Jun 10 '12 at 08:29
  • Cookies are enabled, otherwise session_id would not be set :) No code is before session_start() and session is established successfuly, othervise session_id() function would return empty string. Session ID's from main script and AJAX called scripts are the same. (Tested it with echo, as written in my original question) – StjepanV Jun 10 '12 at 09:19
  • Just sometimes you "might" have a hidden warning that are messing things up. make sure you view all errors and warning and see whats going wrong – Lamis Jun 10 '12 at 09:36
  • I've eaven checked that by sending header info after session_start() so that I can get an warning "Warning: Cannot add header information – headers already sent by (output started at ... )"... – StjepanV Jun 10 '12 at 10:03
  • I bet `session.cookie_httponly` is set to 1. Can you please add `var_dump(session_get_cookie_params());`? – iRaS Feb 02 '17 at 18:00

3 Answers3

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.

Chaitanya Chandurkar
  • 2,142
  • 3
  • 24
  • 43
  • I'm no newbie Chaitanya :) Ofcourse I've tested it with strings :) $_SESSION["test"]="test"; is my testing session variable. – StjepanV Jun 10 '12 at 09:17
1

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?

Marius Balčytis
  • 2,601
  • 20
  • 22
  • I've created completely separate code now to test the PHP settings, and with this code everything is working just fine... So, the error must be with my code. Problem is that my code is complex, long and hard to debug. Thanks to everyone for their input. I'll post the solution when I find it. – StjepanV Jun 10 '12 at 13:15
1

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.

StjepanV
  • 167
  • 2
  • 14