6

I have a session created which is null when called from an ajax call on Safari.

header.php
session_start();
$_SESSION['test'] = 'this is my session';


mypage.php
session_start();
echo $_SESSION['test']; <-- NOT WORKING ON SAFARI

Thanks

steamboy
  • 1,162
  • 5
  • 20
  • 37

4 Answers4

4

I faced the same problem, session_id() kept changing every refresh, so safari wasn't storing the session id in the cookie. By manually adding:

setcookie('PHPSESSID', session_id(), 0, '/');

I got it working.

I.devries
  • 8,747
  • 1
  • 27
  • 29
  • Actually there is a more accurate function, which can set session cookie parameters - [session_set_cookie_params](http://php.net/manual/en/function.session-set-cookie-params.php) – SteveB Nov 20 '14 at 11:21
4

Does it work in other browsers? Does it work in Safari without AJAX? Is this script being loaded from the same domain the original page is on?

Safari apparently has a more conservative cookie policy than other browsers. If everything on the PHP-side works, and other browsers work, I would think that Safari is not sending the session cookie back to the server.

Community
  • 1
  • 1
Reece45
  • 2,771
  • 2
  • 17
  • 19
  • This is generally the case with Safari. It has an extremely restrictive third-party cookie policy which often inteferes with sessions if you are using iframes. I'm willing to bet the Ajax call the OP is making might go to a subdomain or different domain, or has some kind of iframe involved. – zombat Apr 29 '10 at 00:20
  • I've heard Safari 4 is better, but I haven't tested it. – zombat Apr 29 '10 at 00:21
1

Add this line after the session_start() in both the files and tell me if the session id it's the same (that means that you are in the same session).

echo session_id();
Cesar
  • 4,418
  • 2
  • 31
  • 37
0

is it null or is it ""?

when you if(isset($_SESSION['test'])) does it return true?

is this only safari? if so its your browser settings.

Is your session cookie saves sucessfully and you can read that $_session['test'] in the rest of header.php page?

do you call session_start() at the very very beginning of the page?

do you get any errors? if not - you've deffo got them turned on?

Glycerine
  • 7,157
  • 4
  • 39
  • 65