When a cookie is protected with the secure attribute set to true it will not be send by the browser over an unencrypted HTTP
request and thus cannot be observed by an unauthorized person during a man-in-the-middle attack.
When a cookie is created without the secure attribute set to true, browsers will transmit it over unencrypted HTTP
connections as well as HTTPS. An attacker who can observe or intercept network traffic—for example on a public Wi-Fi network—can read the cookie value
in cleartext.
If a session cookie is transmitted over an unencrypted HTTP connection, an attacker who can intercept the traffic can steal it. With a valid session cookie, the attacker can impersonate the victim and gain full access to their account without knowing their password. Even on sites that primarily use HTTPS, a single HTTP request containing the session cookie is enough to expose it.
Set the secure parameter to true when creating cookies to prevent them from being transmitted over unencrypted HTTP
connections.
In php.ini you can specify the flags for the session cookie which is security-sensitive:
session.cookie_secure = 0; // Noncompliant
Same thing in PHP code:
session_set_cookie_params($lifetime, $path, $domain, false); // Noncompliant: this security-sensitive session cookie is created with the secure flag (the fourth argument) set to _false_
If you create a custom security-sensitive cookie in your PHP code:
$value = "sensitive data"; setcookie($name, $value, $expire, $path, $domain, false); // Noncompliant: a security-sensitive cookie is created with the secure flag (the sixth argument) set to _false_
session.cookie_secure = 1;
session_set_cookie_params($lifetime, $path, $domain, true);
$value = "sensitive data"; setcookie($name, $value, $expire, $path, $domain, true);