Cookie
in package
Cookie Class
This class provides methods for setting, getting, deleting, and destroying cookies in PHP.
Table of Contents
Methods
- cookieExists() : bool
- Check if a Cookie Exists
- destroy() : void
- Destroy All Cookies
- get() : mixed|null
- Get Cookie
- set() : bool
- Set Cookie
- unset() : bool
- Delete Cookie
Methods
cookieExists()
Check if a Cookie Exists
public
static cookieExists(string $name) : bool
This function checks whether a cookie with the specified name exists.
Parameters
- $name : string
-
The name of the cookie to check.
Return values
bool —True if the cookie exists, false otherwise.
destroy()
Destroy All Cookies
public
static destroy() : void
Delete all cookies by iterating through them and setting their expiration time to the past.
get()
Get Cookie
public
static get(string $name) : mixed|null
Retrieve the value of a cookie with the specified name.
Parameters
- $name : string
-
The name of the cookie to retrieve.
Return values
mixed|null —The value of the cookie if it exists, or null if the cookie is not set.
set()
Set Cookie
public
static set(string $name, mixed $value[, int $expires = 0 ][, string $path = '/' ][, string $domain = '' ][, bool $secure = true ][, bool $httponly = true ]) : bool
Set a cookie with the specified name, value, and optional parameters.
Parameters
- $name : string
-
The name of the cookie.
- $value : mixed
-
The value to store in the cookie.
- $expires : int = 0
-
The expiration time of the cookie in seconds (default is 0).
- $path : string = '/'
-
The path on the server where the cookie will be available (default is '/').
- $domain : string = ''
-
The domain where the cookie is valid (default is empty).
- $secure : bool = true
-
Indicates if the cookie should only be sent over secure connections (default is true).
- $httponly : bool = true
-
Indicates if the cookie should be accessible only through HTTP (default is true).
Return values
bool —True on success, false on failure.
unset()
Delete Cookie
public
static unset(string $name) : bool
Delete a cookie with the specified name by setting its expiration time to the past.
Parameters
- $name : string
-
The name of the cookie to delete.
Return values
bool —True on success, false on failure.