blog

PHP 8.6 makes sessions safer by default. Your server can have the same settings today

PHP 8.6 came out last Thursday, and the change I like most isn't a new feature. It's three session settings that are now on by default. You don't need to wait for 8.6 to get them, and on most servers you'd be waiting a long time.

Here's what changed, why your server probably won't pick it up on its own, and how to switch them on this week without breaking your logins.

What happened

PHP 8.6.0 was released on 19 November. Tucked into its list of compatibility changes is this: "Sessions are now more secure by default."

Three settings changed, after an RFC that passed without a single vote against:

  • session.use_strict_mode goes from 0 to 1. PHP stops accepting session IDs it didn't create itself. That closes off session fixation, where an attacker plants a session ID on someone and then rides along once they log in.
  • session.cookie_httponly goes from 0 to 1. JavaScript in the page can no longer read the session cookie, so a cross-site scripting bug can't simply steal it.
  • session.cookie_samesite goes from nothing to Lax. Browsers stop sending the session cookie on most requests that start on another site, such as a form posted from elsewhere.

None of this is new technology. All three settings have been in PHP for years. They were just switched off by default, and PHP's own manual says strict mode "is mandatory for general session security."

The team at Dev Partners wrote on Saturday about what PHP 8.6 changes for your app when you upgrade. This post is the server side of it.

Why your server won't get this on its own

Two reasons.

You're probably not on 8.6. Ubuntu 24.04 ships PHP 8.3. Debian 12 ships 8.2, and Debian 13 ships 8.4. Those versions keep the old defaults, and distributions don't jump to a new PHP release mid-life.

Your php.ini sets the old values anyway. The new defaults only apply where nothing overrides them. On Debian and Ubuntu, the packages build /etc/php/8.x/apache2/php.ini, fpm/php.ini and cli/php.ini from PHP's production template. In the 8.3 version of that template, the lines read:

session.use_strict_mode = 0
session.cookie_httponly =
session.cookie_samesite =

The comment just above strict mode even says "enabling it is encouraged." Explicit values win over defaults, so if an old php.ini like this gets carried forward when you do eventually upgrade, you could be on 8.6 with none of the benefit.

What to do this week

1. See what you have now. On the server:

php -i | grep -E 'session.(use_strict_mode|cookie_httponly|cookie_samesite)'

That's the command-line PHP, which can read a different php.ini from your website. For the site itself, look at the cookie it actually sends. Pick a page that starts a session, usually the login page:

curl -sI https://yoursite.example/login | grep -i set-cookie

If the session cookie doesn't mention HttpOnly and SameSite=Lax, the settings are off.

2. Try them on a copy first. These are safe for most sites, but not all. Two things tend to trip up:

  • Logins that come back from another site. Some single sign-on and payment services send the visitor back to you with a form post. With SameSite=Lax the browser won't send your session cookie with it, so the user can look logged out or lose their basket. PHP's migration notes say apps that need this should set None together with session.cookie_secure = 1.
  • Code that makes its own session IDs. Strict mode rejects IDs PHP didn't issue. Custom session handlers and apps that pass session IDs between systems are the usual suspects.

If you have a staging site, this is exactly what it's for. Log in, log out, pay for something, go round every route that involves another service.

3. Switch them on. All three settings can be changed anywhere, so you have a choice. For the whole server, edit each SAPI's php.ini:

session.use_strict_mode = 1
session.cookie_httponly = 1
session.cookie_samesite = "Lax"

For one site on PHP-FPM, put them in that site's pool file instead:

php_admin_value[session.use_strict_mode] = 1
php_admin_value[session.cookie_httponly] = 1
php_admin_value[session.cookie_samesite] = Lax

On shared hosting, a .user.ini file in the site's root usually does the same job. Then restart PHP-FPM or Apache and run the curl check again.

4. Check your framework. If your app is built on a framework, look at its own session config too. Some set these cookie options themselves, which would override whatever you put in php.ini.

5. Make a note for the 8.6 upgrade. When you do move, check your php.ini doesn't still say use_strict_mode = 0. A note in your server docs now saves a puzzled afternoon in two years' time.

The bigger point

Most security news is about something broken that needs fixing. This is the opposite: settings that have quietly been there for years, now on by default because the PHP developers agreed it was time.

It's a twenty-minute job on most servers, plus the testing. If your site is a bespoke PHP app and you're not sure what relies on cross-site logins or home-made session IDs, the team at Dev Partners look after existing software and can check the code before you flip the switch.

And if you'd like someone to look over the whole server, PHP settings included, that's what my health check is for.