2

I have not worked with php for some time and came back to one of my projects just to find out that the session variable is not stored. It came like a surprise, because the last time I checked, it worked and I have not used php (therefore has not changed the code or config).

The situation is as follows: session is not stored across the pages (I can save it and it shows the same on the page, but when I refresh - nothing changes).

I looked here PHP Session data not being saved and replicated the example:

<?php
session_start();
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
echo '<p><a href="page1.php">Refresh</a></p>';

but nothing changes: I still has views = 1.

Nevertheless I have not changed my config, but I still looked at phpinfo(): enter image description here

I checked my permissions to write to the folder /tmp - they are ok, moreover after each refresh a new file appears there.

Then I checked the work in other browsers: and to my surprise it works in firefox: the number correctly increases there.

So the situation: in chrome it does not work, in firefox it works. I use chrome as default. I also remember playing with chrome options when it switches to blink, but as far as I remember I changed back everything.

Any idea how I can fix php sessions in chrome?

P.S:

  • cookies are enabled in chrome
  • sessions files are in /tmp I tried to delete them all and when later I refresh the page in chrome - the new file appears
  • in headers I am sending Cookie:PHPSESSID=mystring but no session id
Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

5 Answers5

2

This is a known issue in Chromium. Issue 114877

Place a favicon in the root folder, and this should correct your issue.

EliteTech
  • 386
  • 3
  • 13
  • yes. unfortunately if you add favicon, you and your users have to visit website for favicon update. chrome history cannot resolve the new favicon from extension. – Mehmet S. May 23 '19 at 13:04
0

Turn on cookies and exit incognito (private browsing) in Chrome.

edit: try setting session.cache_expire back to 180 (the default)

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • @SalvadorDali Uhm, looking at your phpinfo... Should your session_cache_expire be 0 ?! Try putting it back to 180 (the default). You can try doing it in code, without editing php.ini , by placing `session_cache_expire(180);` **before** `session_start();`. The manual specified no default behaviour for setting session.cache_expire to 0, so it might really mean 0 minutes. – nl-x May 13 '13 at 12:30
  • @SalvadorDali But just to doublecheck: FireFox and IE do work ? And in the developer tools you see the page being loaded and getting a 200 - OK ? (Not that it is being served from the cache.) – nl-x May 13 '13 at 12:38
0

I used to come upon some border cases while using php sessions with different browsers.

Can you confirm that the code example is working if using session_write_close() right before the echo statements?

Also you should consider holding your session into another storage rather than the default - in files at the tmp folder. Memcache, redis, database are all options which would scale great.

Nikola Petkanski
  • 4,724
  • 1
  • 33
  • 41
  • but the default should work, and to change to other storage just because this isn't working is a bit awkward... – Nanne May 13 '13 at 12:53
  • I've mentioned a consideration based on my experience with using sessions. I think there is no experienced php developer who would prefer to save its sessions into the default tmp folder. – Nikola Petkanski May 13 '13 at 12:57
  • Note that this is just an advice - it won't fix the issue he complains about. The solution I gave is to try ```session_write_close()```. – Nikola Petkanski May 13 '13 at 12:59
0

Try to comment session.save_path = "/tmp" directive in php.ini using prefix ; .

So after restart apache, phpinfo() should be show no value for session_save_path

Amir
  • 4,089
  • 4
  • 16
  • 28
  • How's that a fix for the question: ```Any idea how I can fix php sessions in chrome?``` – Nikola Petkanski May 13 '13 at 13:00
  • cokies send by the PHP server to the browser and it saves in `session.save_path`, but perhaps your browser `chrome` have not access to the path – Amir May 13 '13 at 13:18
  • The `save_path` relates to where the PHP stores the physical file for the session on the server. No browser will have access to that path. – PeerBr Apr 07 '15 at 02:44
  • @PeerBr, cookie (contain session_id) stores in client side and session data stores in server side at `session.save_path`. it is better to not change this path. – Amir Apr 07 '15 at 11:42
0

Please check session use cookie in php.ini. If it is zero activate it to 1 like session.use_cookies = 1