doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

setcookie() invalid options cause ValueError (not E_WARNING) since PHP 8.0

Open mahyard opened this issue 2 months ago • 0 comments

Affected page

https://www.php.net/manual/en/function.setcookie.php

Issue description

The setcookie() documentation states:

Parameters: options: An associative array which may have any of the keys expires, path, domain, secure, httponly and samesite. If any other key is present an error of level E_WARNING is generated.

However, since PHP 8.0, passing an invalid key in the options array (e.g., ['lifetime' => 3600]) throws a ValueError instead of emitting a warning:

Fatal error: Uncaught ValueError: setcookie(): option "lifetime" is invalid

This behavior change appears intentional as part of PHP 8.0’s consistent parameter validation updates, but the manual has not been updated to reflect it.

Steps to reproduce

  1. Create a PHP file with the following contents:
<?php
try {
    setcookie('my-cookie', 'some-value', ['lifetime' => 3600]);
} catch (ValueError $e) {
    die("Caught ValueError: " . $e->getMessage());
}
echo "No ValueError caught. Just an E_WARNING.";
  1. Run it under PHP 7.4 and open it in a browser (or use php -S localhost:8000 and visit it).

You’ll get an E_WARNING but the page will continue to render.

  1. Switch to PHP 8.0 and reload the page.

It now throws a ValueError, and the catch block is triggered.

Suggested fix

Update the note about invalid keys to state that a ValueError is thrown in PHP ≥ 8.0.

mahyard avatar Nov 06 '25 08:11 mahyard