PHP setrawcookie()Functions
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 into the user's computer. This cookie is sent whenever the same computer requests a page through a browser.
The cookie's name is automatically assigned as a variable with the same name. For example, if a cookie named "user" is sent, a variable named $user is automatically created containing the cookie's value.
The cookie must be assigned before any other output is sent to the client.
Returns TRUE on success, or FALSE on failure.
Syntax
| Parameter | 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()+3600*24*30 will set the cookie to expire in 30 days. If this parameter is not set, the cookie will automatically expire when the session ends (i.e., when the browser is closed). |
| path | Optional. Specifies the server path of the cookie. If the path is set to "/", the cookie will work within the entire domain. If the path is set to "/test/", the cookie will work in the test directory and all its subdirectories. The default path value is the current directory where the cookie is located. |
| domain | Optional. Specifies the domain of the cookie. To make the cookie work in 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 will only work in the www subdomain. |
| secure | Optional. Specifies whether a secure HTTPS connection is required to transmit the cookie. Set to TRUE if the cookie needs to be transmitted over a secure HTTPS connection. Default is FALSE. |
Tips and Notes
Tip:You can access the value of a cookie named "user" via $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"].
Note:The setrawcookie() function is almost identical to the setcookie() function, except that it does not automatically URL-encode the cookie value when sent to the client.
Example 1
Set and send a cookie:
$value = "my cookie value";
// send a simple cookie
setrawcookie("TestCookie",$value);
?>
<html>
<body>
...
...
$value = "my cookie value";
// send a cookie that expires in 24 hours
setrawcookie("TestCookie",$value, time()+3600*24);
?>
<html>
<body>
...
...
Example 2
Different ways to retrieve a cookie value (after the cookie has been set):
<body>
<?php
// Print individual cookies
echo $_COOKIE["TestCookie"];
echo "<br />";
echo $HTTP_COOKIE_VARS["TestCookie"];
echo "<br />";
// Print all cookies
print_r($_COOKIE);
?>
</body>
</html>
The code above will output:
my cookie value
Array ([TestCookie] => my cookie value)
Example 3
Delete a cookie by setting its expiration date to a past date/time:
// Set the expiration date to one hour ago
setrawcookie ("TestCookie", "", time() - 3600);
?>
<html>
<body>
...
...
Example 4
Create an array cookie:
setrawcookie("cookie[three]","cookiethree");
setrawcookie("cookie[two]","cookietwo");
setrawcookie("cookie[one]","cookieone");
// print cookies (after reloading page)
if (isset($_COOKIE["cookie"]))
{
foreach ($_COOKIE["cookie"] as $name => $value)
{
echo "$name : $value <br />";
}
}
?>
<html>
<body>
...
...
The code above will output:
two : cookietwo
one : cookieone
Complete PHP HTTP Reference Manual Other Extensions