Using cookies in PHP 7 involves leveraging the setcookie()
function to send cookies from the server to the client's browser and retrieving them using the $_COOKIE
superglobal array. The setcookie()
function takes several arguments:
name
(required): The name of the cookie. This should be a string and should ideally be descriptive.value
(required): The value of the cookie. This can be a string, integer, or boolean; however, it will be treated as a string.expire
(optional): A Unix timestamp specifying the cookie's expiration time. If omitted, the cookie will be a session cookie, meaning it's only valid for the current browser session and will be deleted when the browser closes. If provided, the cookie becomes persistent.path
(optional): The path on the server in which the cookie will be available. Defaults to the current directory. Setting this to /
makes the cookie available across the entire domain.domain
(optional): The domain for which the cookie is valid. Omitting this means the cookie is only valid for the current domain. Setting this allows cookies to be shared across subdomains.secure
(optional): If set to true
, the cookie will only be transmitted over HTTPS. This is crucial for security.httponly
(optional): If set to true
, the cookie will only be accessible through HTTP requests, preventing access via JavaScript. This is a vital security measure to mitigate XSS attacks.Example: Setting a persistent cookie named "username" with a value of "john_doe" that expires in one year:
<?php $expire = time() + 31536000; // One year from now setcookie("username", "john_doe", $expire, "/", ".example.com", true, true); ?>
Retrieving the cookie's value:
<?php if (isset($_COOKIE["username"])) { echo "Welcome, " . $_COOKIE["username"] . "!"; } ?>
Security is paramount when using cookies. Several crucial considerations must be addressed:
secure
flag in setcookie()
is essential here.httponly
flag: Setting the httponly
flag prevents JavaScript from accessing the cookie, mitigating Cross-Site Scripting (XSS) attacks. This is a crucial security best practice.SameSite
attribute to Strict
or Lax
to prevent CSRF attacks. The SameSite
attribute controls whether a cookie is sent with cross-site requests.urlencode()
or similar functions to sanitize input before storing it in a cookie.The key difference between session and persistent cookies lies in the expire
parameter of the setcookie()
function:
Session Cookies: Omit the expire
parameter or set it to a time in the past. These cookies are only valid for the duration of the browser session and are automatically deleted when the browser closes.
<?php $expire = time() + 31536000; // One year from now setcookie("username", "john_doe", $expire, "/", ".example.com", true, true); ?>
Persistent Cookies: Provide a future Unix timestamp for the expire
parameter. This makes the cookie persist on the client's machine until the specified expiration date.
<?php if (isset($_COOKIE["username"])) { echo "Welcome, " . $_COOKIE["username"] . "!"; } ?>
Retrieving cookies is the same for both types: Use the $_COOKIE
superglobal array.
Expiration: Persistent cookies expire automatically at the time specified by the expire
parameter in setcookie()
.
Deletion: To delete a cookie, set its value to an empty string and set the expire
parameter to a time in the past (e.g., time() - 3600
). Keep the other parameters (path, domain) consistent with how the cookie was originally set.
<?php $expire = time() + 31536000; // One year from now setcookie("username", "john_doe", $expire, "/", ".example.com", true, true); ?>
This effectively removes the cookie from the client's browser. Remember that the browser might still hold the cookie for a short time before actually deleting it, depending on its caching mechanisms. Also, ensuring the path and domain match the original setcookie()
call is crucial for successful deletion.
The above is the detailed content of How to Use Cookies in PHP 7?. For more information, please follow other related articles on the PHP Chinese website!