Skip to content
PHP Logo

How can PHP developers ensure that cookies are set and read securely to prevent unauthorized tracking of user activity?

To ensure cookies are set and read securely in PHP, developers should always set the 'secure' and 'httponly' flags when creating cookies. The 'secure' flag ensures that the cookie is only sent over HTTPS connections, while the 'httponly' flag prevents the cookie from being accessed through client-side scripts, reducing the risk of cross-site scripting attacks.

php
// Set a cookie with the 'secure' and 'httponly' flags
setcookie('cookie_name', 'cookie_value', time() + 3600, '/', 'example.com', true, true);