A PHP application can have secure authentication logic and still expose users to session-related attacks because of how session cookies are configured.
For years, PHP shipped with several session settings disabled or unset by default. Developers could enable them manually, but applications that relied on the defaults often missed important protections.
PHP 8.6 changes that.
Released on November 19, 2026, PHP 8.6 enables stricter session ID validation, prevents JavaScript from directly reading session cookies, and explicitly applies the SameSite=Lax cookie policy.
These changes make session handling safer without requiring developers to introduce a new authentication library or rewrite their session-management code.
The changes are small. Their implications are worth understanding before upgrading.
1. What Changed in PHP 8.6?
PHP 8.6 changes the default values of three session configuration directives.
| Configuration | Before PHP 8.6 | PHP 8.6 |
|---|---|---|
session.use_strict_mode | 0 | 1 |
session.cookie_httponly | 0 | 1 |
session.cookie_samesite | Unset | Lax |
Each setting addresses a different security concern.
- Strict mode helps prevent session fixation.
- HttpOnly reduces direct session-cookie theft through JavaScript.
- SameSite=Lax restricts when browsers send session cookies in cross-site requests.
These protections work at different stages of session handling. None of them independently prevents every form of session hijacking.
2. Strict Session Mode: Reject Unrecognized Session IDs
A session ID identifies a user's session on the server. The browser usually stores that identifier in a cookie and sends it with subsequent requests.
The problem arises when an application accepts a session ID supplied by a client without checking whether that ID represents an existing session.
This can create an opportunity for session fixation.
Consider this scenario:
- An attacker obtains or creates a session ID.
- The attacker somehow causes a victim's browser to use that ID.
- The victim logs in.
- The application associates the authenticated session with the known ID.
- The attacker attempts to reuse that ID.
If the session remains valid, the attacker may gain access to the victim's authenticated session.
Before PHP 8.6, strict session mode was disabled by default:
session.use_strict_mode = 0
PHP 8.6 changes the default to:
session.use_strict_mode = 1
When strict mode is enabled, PHP validates a supplied session ID instead of automatically accepting an unknown identifier.
With the built-in file-based session handler, PHP checks whether the corresponding session already exists. If it does not, PHP generates a new session ID.
This makes it harder for an attacker to force an application to adopt an arbitrary session identifier.
However, strict mode is a mitigation, not a replacement for proper session ID regeneration after authentication.
What should developers know?
Applications using custom session handlers need particular attention.
A custom handler must provide appropriate session ID generation and validation behavior. PHP 8.6 also deprecates custom session handlers that do not implement both create_sid() and validateId().
Simply enabling strict mode does not guarantee meaningful validation if a custom handler accepts every supplied ID.
If your application uses Redis, Memcached, or a custom database-backed session handler, verify how its handler implements these operations before upgrading.
3. HttpOnly Cookies: Stop JavaScript from Reading Session IDs
Another important change concerns the HttpOnly cookie attribute.
By default, browsers expose ordinary cookies to JavaScript through:
document.cookie
That creates a problem when an application allows its session cookie to be accessed through the same interface.
Suppose an attacker exploits a cross-site scripting (XSS) vulnerability and injects JavaScript into a trusted page.
Without HttpOnly, the injected script may be able to read the session cookie and send its value to an external server.
The attacker can then attempt to reuse the stolen session ID.
Before PHP 8.6:
session.cookie_httponly = 0
PHP 8.6 changes the default to:
session.cookie_httponly = 1
The browser now prevents JavaScript from reading the session cookie through document.cookie.
The cookie is still sent automatically with eligible HTTP requests. HttpOnly changes JavaScript access, not normal browser cookie transmission.
Does HttpOnly prevent XSS attacks?
No.
HttpOnly protects the cookie value from direct JavaScript access, but it does not remove the underlying XSS vulnerability.
An injected script may still perform actions using the victim's authenticated browser session, even without knowing the actual session ID.
Developers must continue to validate input, escape output, apply an appropriate Content Security Policy, and prevent untrusted content from executing as code.
HttpOnly reduces one important consequence of XSS. It does not make an application immune to it.
4. SameSite=Lax: Restrict Cross-Site Session Requests
The third change introduces an explicit SameSite policy for PHP session cookies.
A browser normally attaches eligible cookies to requests sent to a website. Without appropriate restrictions, another website can attempt to trigger requests using a user's existing session.
This is one of the problems behind Cross-Site Request Forgery (CSRF).
For example:
A user is logged into an online banking application. The session cookie is stored in their browser.
The user then visits a malicious website containing a form that attempts to submit a request to the banking application.
If the browser sends the banking session cookie with that cross-site request, the application may receive a request that appears to come from an authenticated user.
The SameSite attribute helps control this behavior.
Before PHP 8.6, the session cookie did not explicitly set a SameSite value by default.
PHP 8.6 changes it to:
session.cookie_samesite = Lax
With SameSite=Lax, browsers send the session cookie on same-site requests and allow it on certain top-level cross-site navigations using safe HTTP methods, such as GET.
They generally withhold it from cross-site POST requests and cross-site subresource requests.
This reduces the browser's willingness to attach an authenticated session to requests initiated by unrelated websites.
Lax vs Strict vs None
Developers should understand the difference before choosing a policy.
| Policy | Cross-site cookie behavior |
|---|---|
Strict | Blocks cookies on cross-site requests, including top-level navigation |
Lax | Allows eligible top-level safe-method navigation, but blocks cross-site POST requests |
None | Allows cross-site cookie delivery when the browser's other cookie requirements are satisfied |
Lax is a reasonable default for many web applications because it provides additional protection without blocking every cross-site navigation.
None is sometimes necessary for integrations that rely on cross-site cookies, but it must be paired with Secure.
This distinction matters for applications using SAML authentication, external payment providers, embedded applications, or cross-origin form submissions.
A developer should not change the entire session policy to None just because one integration requires cross-site cookies. Evaluate the affected authentication flow and use a dedicated, appropriately scoped session or token-based design where possible.
5. What Has Not Changed: Secure Cookies Still Need HTTPS Configuration
There is an important detail developers should not overlook.
PHP 8.6 changes three session defaults, but it does not automatically enable the Secure cookie attribute.
The relevant setting remains:
session.cookie_secure = 0
The Secure attribute tells the browser to send the cookie only over HTTPS connections.
Without it, an otherwise protected session cookie may still be transmitted over an insecure HTTP connection if the application is accessible that way.
For a production website served exclusively over HTTPS, configure:
session.cookie_secure = 1
A practical session configuration for an HTTPS application is:
session.use_strict_mode = 1
session.cookie_httponly = 1
session.cookie_samesite = Lax
session.cookie_secure = 1
The first three values match PHP 8.6's new defaults. The last one is an additional production security setting that developers should configure explicitly.
6. What Developers Should Check Before Upgrading
The new defaults improve security, but existing applications may depend on the previous behavior.
A PHP version upgrade should therefore include a session-configuration review.
Check custom session handlers.
Applications using session_set_save_handler() should verify that their handlers implement session ID creation and validation correctly.
The new deprecations are particularly relevant to older custom handlers that have not been updated.
Review cross-site authentication flows.
Check whether your application expects a session cookie during a cross-site POST request.
SAML single sign-on callbacks and some payment integrations are common examples. A change to SameSite=Lax can affect these flows.
Remove JavaScript dependencies on PHP session IDs.
Some older applications read the session cookie from JavaScript and copy its value into custom request headers.
That behavior will stop working with HttpOnly enabled.
The better approach is to keep session IDs server-managed and use a separate CSRF token when the client needs a token for request verification.
Check the effective PHP configuration.
Do not assume that installing PHP 8.6 automatically guarantees the new values are active.
An existing php.ini or deployment configuration may explicitly override the new defaults.
Check the settings used by the actual web runtime, not only the command-line PHP installation.
For example:
<?php
$settings = [
'session.use_strict_mode',
'session.cookie_httponly',
'session.cookie_samesite',
'session.cookie_secure',
];
foreach ($settings as $setting) {
echo $setting . ' = ' . ini_get($setting) . PHP_EOL;
}
The output helps confirm the effective values for the PHP process executing that script. Remove temporary diagnostic scripts after testing.
7. Session Security Still Requires More Than Configuration
The PHP 8.6 changes are useful, but secure session management involves additional application-level responsibilities.
One important practice is regenerating the session ID when a user authenticates or gains additional privileges.
For example:
<?php
session_start();
if ($authenticated) {
session_regenerate_id();
$_SESSION['user_id'] = $user['id'];
$_SESSION['authenticated'] = true;
}
Regenerating the identifier before storing the authenticated state helps prevent an existing unauthenticated session ID from continuing into an authenticated session.
Applications should also implement appropriate session expiration, logout handling, CSRF protection, and server-side authorization checks.
A valid session cookie should never be treated as proof that every requested operation is permitted.
Session management is one layer of application security, not the entire security model.
Conclusion
PHP 8.6 makes an important improvement to the security baseline of PHP applications.
By enabling strict session ID validation, HttpOnly cookies, and an explicit SameSite=Lax policy, it reduces exposure to several common session-related attacks without introducing a new session API.
The upgrade is relatively small, but developers should pay attention to custom session handlers and cross-site authentication workflows.
There is also one detail worth remembering: Secure cookies are still the application's responsibility.
The broader improvement is straightforward. PHP is moving toward safer defaults, so developers no longer need to remember to enable three widely recommended session protections manually. But those defaults work best when the rest of the application's authentication and session-management logic is designed with the same care.



