# PHP Cookies Reference > Source: ## What is a Cookie? A cookie is often used to identify a user. It is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values. ## Create a Cookie with `setcookie()` A cookie is created with the `setcookie()` function. ### Syntax ```php setcookie(name, value, expire, path, domain, secure, httponly); ``` ### Parameters | Parameter | Description | |------------|---------------------------------------------------------------------------------------------------------| | `name` | Required. Specifies the name of the cookie. | | `value` | Optional. Specifies the value of the cookie. | | `expire` | Optional. Specifies when the cookie expires. The value `time() + 86400 * 30` will set the cookie to expire in 30 days. If this parameter is omitted or set to `0`, the cookie will expire at the end of the session (when the browser closes). Default is `0`. | | `path` | Optional. Specifies the server path of the cookie. If set to `"/"`, the cookie will be available within the entire domain. If set to `"/php/"`, the cookie will only be available within the `php` directory and all sub-directories of `php`. The default value is the current directory that the cookie is being set in. | | `domain` | Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of `example.com`, set domain to `".example.com"`. | | `secure` | Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. `true` means the cookie will only be set if a secure connection exists. Default is `false`. | | `httponly` | Optional. If set to `true` the cookie will be accessible only through the HTTP protocol (the cookie will not be accessible by scripting languages, such as JavaScript). This setting can help to reduce identity theft through XSS attacks. Default is `false`. | **Note:** The `setcookie()` function must appear BEFORE the `` tag (before any output is sent to the browser). ### Example: Create a Cookie The following example creates a cookie named "user" with the value "John Doe". The cookie will expire after 30 days. The `"/"` means that the cookie is available across the entire website: ```php "; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> ``` **Note:** The `setcookie()` function sends the cookie as part of the HTTP response header. A cookie is not visible to the current page until the next loading of a page that the cookie should be visible for. So to test the cookie, the page must be reloaded or another page must be navigated to. ## Retrieve a Cookie Value The PHP `$_COOKIE` superglobal variable is used to retrieve a cookie value. ```php "; echo "Value is: " . $_COOKIE["user"]; } ?> ``` **Tip:** Use the `isset()` function to find out if a cookie is set before attempting to access its value. ## Modify a Cookie Value To modify a cookie, just set (again) the cookie using the `setcookie()` function: ```php "; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> ``` ## Delete a Cookie To delete a cookie, use the `setcookie()` function with an expiration date in the past: ```php ``` ## Check if Cookies are Enabled The following example creates a small script that checks whether cookies are enabled. First, try to create a test cookie with the `setcookie()` function, then count the `$_COOKIE` array variable: ```php 0) { echo "Cookies are enabled."; } else { echo "Cookies are disabled."; } ?> ```