PHP setrawcookie() Function
Complete PHP HTTP Reference
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 typically a small text 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.
The name of the cookie is automatically assigned to a variable of the same
name. For example, if a cookie was sent with the name "user", a variable is
automatically created called $user, containing the cookie value.
A cookie must be assigned before
any other output is sent to the client.
This function returns TRUE on success or FALSE on failure.
Syntax
setrawcookie(name,value,expire,path,domain,secure)
|
Parameter |
Description |
name |
Required. Specifies the name of the cookie |
value |
Required. Specifies the value of the cookie |
expire |
Optional. Specifies when the cookie expires.
time()+3600*24*30 will set the cookie to expire in 30 days. If this
parameter is not set, the cookie will expire at the end of the session (when
the browser closes). |
path |
Optional. Specifies the server path of the cookie If set
to "/", the cookie will be available within the entire domain. If set to
"/test/", the cookie will only be available within the test directory and
all sub-directories of test. 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 then you'd set it
to ".example.com". Setting it to www.example.com will make the cookie only
available in the www subdomain |
secure |
Optional. Specifies whether or not the cookie should only
be transmitted over a secure HTTPS connection. TRUE indicates that the
cookie will only be set if a secure connection exists. Default is FALSE. |
Tips and Notes
Tip:
The value of a cookie named "user" can be accessed by $HTTP_COOKIE_VARS["user"]
or by $_COOKIE["user"].
Note: The setrawcookie() function is exactly the same as setcookie()
except that the cookie value will not be automatically URL encoded when sent to
the client.
Example 1
Set and send cookie examples:
<?php
$value = "my cookie value";
// send a simple cookie
setrawcookie("TestCookie",$value);
?>
<html>
<body>
...
...
|
<?php
$value = "my cookie value";
// send a cookie that expires in 24 hours
setrawcookie("TestCookie",$value, time()+3600*24);
?>
<html>
<body>
...
...
|
Example 2
Different ways of retrieving the value of the cookie (after the cookie has
been set):
<html>
<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 output of the code above will be:
my cookie value
my cookie value
Array ([TestCookie] => my cookie value)
|
Example 3
Delete a cookie by setting the expiration date to a date/time in the past:
<?php // Set the expiration date to one hour ago setrawcookie ("TestCookie", "", time() - 3600); ?>
<html>
<body>
...
...
|
Example 4
Create an array cookie:
<?php
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 output of the code above will be:
three : cookiethree
two : cookietwo
one : cookieone
|
Complete PHP HTTP Reference
Make your web applications look like a million bucks
|
|
Most web applications today use boring methods to present data to their viewers using grids or simple HTML tables. FusionCharts induces "life" into the web applications by converting monotonous data into lively charts, gauges & maps.
FusionCharts works with all technologies like ASP, ASP.NET, PHP, ColdFusion, Ruby on Rails, JSP, HTML pages etc.
and connects to any database to render animated & interactive charts. It takes less than 15 minutes and no expertise
whatsoever to build your first chart and just a glance of it to captivate your audience. This fact is endorsed by our
12,000 customers and 150,000 users which include a majority of the Fortune 500 companies.
And yeah, your applications could look like a million bucks by spending just $69.
So go ahead, download your
copy of FusionCharts and start "wow-ing" your customers now!
|
|