PHP setrawcookie() Function
Complete PHP HTTP Reference Manual
Definition and Usage
The setrawcookie() function sends an HTTP cookie without URL-encoding the cookie value.
A cookie is a variable sent by the server to the browser. A cookie is usually a small text file that the server embeds on the user's computer. Whenever the same computer requests a page through the browser, the cookie is sent.
The cookie's name is automatically assigned to a variable of the same name. For example, if a cookie named "user" is sent, a variable named $user is automatically created containing the cookie's value.
Cookies must be assigned before any other output is sent to the client.
Returns TRUE on success, or FALSE on failure.
Syntax
setrawcookie(name,value,expire,path,domain,secure)
| Parameters | Description |
|---|---|
| name | Required. Specifies the name of the cookie. |
| value | Required. Specifies the value of the cookie. |
| expire | Optional. Specifies the expiration time of the cookie.
time()+36002430 will set the cookie to expire in 30 days. If this parameter is not set, the cookie expires automatically when the session ends (that is, when the browser is closed). |
| path | Optional. Specifies the server path of the cookie.
If the path is set to "/", the cookie is valid within the entire domain. If the path is set to "/test/", the cookie is valid in the test directory and all its subdirectories. The default path value is the current directory where the cookie is set. |
| domain | Optional. Specifies the domain of the cookie.
To make a cookie valid across all subdomains of example.com, you need to set the cookie's domain to ".example.com". When you set the cookie's domain to www.example.com, the cookie is only valid in the www subdomain. |
| secure | Optional. Specifies whether the cookie should be transmitted over a secure HTTPS connection. Set to TRUE if the cookie needs to be transmitted over a secure HTTPS connection. Default is FALSE. |
Tips and Notes
**Tip:** The value of a cookie named "user" can be accessed via $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"].
**Note:** setrawcookie() is almost identical to setcookie(), except that the cookie value is not automatically URL-encoded when sent to the client.
Example 1
Set and send a cookie:
...
...
...
...
Example 2
Different ways to retrieve a cookie value (after the cookie is set):
"; echo $HTTP_COOKIE_VARS["TestCookie"]; echo ""; // Print all cookies print_r($_COOKIE); ?>
The code above will output:
my cookie value
my cookie value
Array ([TestCookie] => my cookie value)
Example 3
Delete a cookie by setting its expiration date to a past date/time:
...
...
Example 4
Create an array cookie:
$value) { echo "$name : $value"; } } ?>
...
...
The code above will output:
three : cookiethree
two : cookietwo
one : cookieone
Complete PHP HTTP Reference Manual