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
0

6 Answers 6

2

This is a known issue in Chromium. Issue 114877

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

3 Comments

but the default should work, and to change to other storage just because this isn't working is a bit awkward...
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.
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().
0

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

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

2 Comments

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

4 Comments

How's that a fix for the question: Any idea how I can fix php sessions in chrome?
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
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, 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.
0

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

Comments

0

The way I fixed it was, removing the www from the domain name here:

session_set_cookie_params([
  'lifetime' => (365 * 24 * 60 * 60),
  'path' => '/',
  'domain' => 'demo.com',
  'secure' => true,
  'httponly' => true,
  'samesite' => 'Strict',
]);

after that it works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.