45: Audit PHP 8.6 Session Cookie Defaults
Overview
Section titled “Overview”Inspect the new defaults for session ID validation and cookie handling. Review your application’s explicit configuration before upgrading.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. - The
sessionextension. Run this example withphp -nto see the built-in defaults. - Review the previous chapter if you’re following the series in order.
Allow about 10 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable session-defaults.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php -n code/php-8-6/45-session-defaults/session-defaults.php<?php
declare(strict_types=1);
printf("Strict mode: %s\n", ini_get('session.use_strict_mode'));$cookie = session_get_cookie_params();printf("HttpOnly: %s\n", $cookie['httponly'] ? 'yes' : 'no');printf("SameSite: %s\n", $cookie['samesite']);Expected Result
Section titled “Expected Result”Strict mode: 1HttpOnly: yesSameSite: LaxWhy It Works
Section titled “Why It Works”Strict mode rejects uninitialized IDs when the handler supports validation. HttpOnly prevents script access to the session cookie. SameSite=Lax affects cross-site requests such as embedded pages and cross-site POSTs. If a flow intentionally needs cross-site cookies, use SameSite=None together with Secure and HTTPS and test it in a browser.
Exercise
Section titled “Exercise”Run the sample on PHP 8.5 with -n, if available, and compare the built-in values. Then check the values under the actual web SAPI and its INI configuration.
Wrap-up
Section titled “Wrap-up”Continue with Validate IDs in Custom Session Handlers.