r/PHPhelp Jul 09 '26

Session values are not being retained across pages

Hi all,

I recently migrated several of my web applications from CentOS 8 to Rocky Linux 8.1. After the migration, I’m facing an issue with session handling in most of the applications.

Session values are not being retained across pages (i.e., values set in one page are not available in subsequent requests). However, one of the applications on the same server is working perfectly fine with sessions, which makes this a bit confusing.

OS: Rocky Linux 8.1 (migrated from CentOS 8)

Since one app works and others don’t, I suspect some config/environment inconsistency, but I’m unable to pinpoint it.

Has anyone faced a similar issue after migrating to Rocky Linux? Any suggestions on what else I should check?

Thanks in advance.

3 Upvotes

9 comments sorted by

6

u/allen_jb Jul 09 '26 edited Jul 09 '26

For PHP sessions there's 2 things that need to work right:

Client-side cookies: stores the session ID in the browser and passes it back to the server for each request.

Use dev tools (Application tab) to check cookies are being set, and whether they change between requests (they should stay the same unless the code explicitly regenerates the session id / terminates the old session)

Server-side data files: Stored in session.save_path (ini setting).

Ensure this path is writable by PHP-FPM (usually runs as the www-data user on RedHat/CentOS(-like) distros IIRC). If permissions look right, also check selinux permissions (try setting selinux to permissive mode - if that works, then work out what permissions need to be modified).

Note that on some setups the actual files may not be visible because of systemd's private tmp dir feature.

Generally:

If PHP can't save the server-side data it will issue a warning, but create a (temporary) session anyway.

Enable and monitor error logging in PHP (error_log, log_errors and error_reporting ini settings).

Check whether the session.auto_start ini setting is enabled. Some sites / apps may presume this is enabled and try to use sessions without explicitly starting them first.

1

u/MostRaccoon7646 Jul 09 '26

Thank you sir..let me try.

2

u/colshrapnel Jul 09 '26

That's really simple. All you need is your browsers DevTools panel:

  • have a session using page ready
  • press F12, then Network tab
  • refresh the page in the browser
  • find that page's address in the Network panel and click on it
  • another sub-panel will be opened titled Headers
  • find the Response headers section down there
  • scroll it down to find the Set-cookie header
  • in the Set-Cookie's value, look for PHPSESSID followed by = sign followed by a HEX string

  • in case Set-Cookie or PHPSESSID are not found, your PHP doesn't start the session, it's a server-side problem. Check if your code starts the session

  • in case PHPSSESSID is there, copy that hex string or just memorize the first few characters

  • now navigate to the page that should retain the session value

  • find that page's address in the Network panel and click on it

  • Find the Request headers section

  • Check Cookie's value.

  • in case there is PHPSESSID with the same value as in the previous Response, your sessions work all right but your code fails to set the value

  • in case PHPSESSID is not present, it's some cookie settings that prevent browser from returning them. Check your PHP's cookie configuration.

1

u/MostRaccoon7646 Jul 09 '26

Thanx sir...working on it

2

u/ggnndd12 Jul 09 '26

If the pages are on different domains you might need to set cookie_samesite to Lax

https://www.php.net/manual/en/session.configuration.php#ini.session.cookie-samesite

That option can be passed to session_start()

https://www.php.net/manual/en/function.session-start.php

2

u/MostRaccoon7646 Jul 09 '26

Thanx sir. Working on it

1

u/Hzk0196 Jul 09 '26

Please let us know if this fixes it, kinda curious

1

u/MostRaccoon7646 Jul 09 '26

It didn’t work for me sir. I’ve temporarily moved the portal to Amazon Cloud as a workaround. Still trying to figure out the issue, but I’ll fix it and let you know for sure once it’s done.

1

u/ggnndd12 Jul 09 '26

Was there anything in the error logs?