Latest web development tutorials
 

PHP setrawcookie() Function

< PHP HTTP Reference

Example

The following example creates a cookie with PHP. The cookie is named "user" and the value will be "John Doe". The cookie value will not be URL encoded. The cookie will expire after 30 days (86400 * 30). Using "/", means that the cookie is available in entire website (otherwise, select the directory you prefer):

<?php
$cookie_name = "user";
$cookie_value = "John";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
?>
<html>
<body>

<?php
echo "Cookie is set.";
?>

</body>
</html>
?>
Run example »

Definition and Usage

The setrawcookie() function defines a cookie (without URL encoding) to be sent along with the rest of the HTTP headers.

A cookie is often used to identify a user. A cookie 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.

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.

Note: The setrawcookie() function must appear BEFORE the <html> tag.

Note: To automatically URL-encode the cookie value when sending, and automatically decode when receiving, use the setcookie() function instead.

Syntax

setrawcookie(name,value,expire,path,domain,secure);

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 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 "/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". 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.

Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 5+

More Examples

Example

Retrieve the value of the cookie named "user" (using the global variable $_COOKIE). Also use the isset() function to find out if the cookie exists:

<html>
<body>

<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
    echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>
Run example »

Example

To modify a cookie, just set (again) the cookie using the setrawcookie() function:

<?php
$cookie_name = "user";
$cookie_value = "Alex";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
    echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>
Run example »

Example

To delete a cookie, use the setrawcookie() function with an expiration date in the past:

<?php
$cookie_name = "user";
unset($_COOKIE[$cookie_name]);
// empty value and expiration one hour before
$res = setrawcookie($cookie_name, '', time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>
Run example »

Example

Create a small script that checks whether cookies are enabled. First, try to create a test cookie with the setrawcookie() function, then count the $_COOKIE array variable:

<?php
setrawcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>

<?php
if(count($_COOKIE) > 0) {
    echo "Cookies are enabled";
} else {
    echo "Cookies are disabled";
}
?>

</body>
</html>
Run example »

< PHP HTTP Reference