Database design: keys, relationships and normalization
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
Small values stored in the browser, and the flags that make them safer.
<?php
setcookie('theme', 'dark', [
'expires' => time() + 60 * 60 * 24 * 30, // 30 days
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax',
]);Call setcookie() before any HTML output.
$theme = $_COOKIE['theme'] ?? 'light';The cookie is available on the next request, not the one that set it.
Set an expiry date in the past:
setcookie('theme', '', ['expires' => time() - 3600, 'path' => '/']);httponly: JavaScript cannot read the cookie, which limits damage from XSS.secure: only sent over HTTPS.samesite: limits sending the cookie with requests from other sites.Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
A folder layout and a few habits that keep a growing PHP site tidy, secure and easy to maintain.