PHP Cookies
PHP cookies are a way to store small pieces of data on the user's computer, which can persist across page loads and sessions. They’re often used for things like remembering login info, preferences, or tracking user behavior.
Setting a Cookie
Use setcookie()
before any HTML output:
Parameters:
-
name
: Cookie name -
value
: Value to store -
expire
: Timestamp (e.g.,time() + 3600
= 1 hour from now) -
path
: Scope of the cookie (default is current directory) -
domain
: Domain the cookie is available to -
secure
: Only send over HTTPS -
httponly
: Inaccessible via JavaScript (for security)
Accessing a Cookie
Cookies are available in the $_COOKIE
superglobal:
Deleting a Cookie
To delete a cookie, set its expiration time in the past: