Papermashup

Subscribe


Tweets


"RT @cpray86: Good to see the receptionist's Jpeg of an Excel file is coming along nicely... #TheApprentice https://t.co/uiPGhf64U3"

@ashleyford 1 day ago

"Translation wrong for this paper it should be: Bitte nicht den Horror-Clown (Please not the Horror-clown) https://t.co/RYOD6aND6s via @bandt"

@ashleyford 3 weeks ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Papermashup

PHP Cookies

Not the cookies you enjoy eating

AshleyAshley

We will be looking at using cookies in PHP, not the cookie you enjoy eating, but the cookie as a tool for web development. We will examine what a cookie is, discuss how to create one, learn about how to delete and modify them.
In web dev a cookie is a tool used to identify a user. A cookie is a small file that is embedded by the server on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, we can create and retrieve cookie values.

Creating Cookies with PHP

To create a cookie, the setcookie(); function is used.

The cookie creation syntax is:


setcookie(name, value, expire, path, domain, secure, httponly);

Note: The “name” parameter is compulsory, while the other parameters are optional.

In the following example, a cookie named “userDetails” will be created. The value “Alan Smith” will be assigned to the cookie. The cookie will be set to expire after 7 days (86400 * 7). The “/” means that the cookie is available across theĀ entire website (otherwise, select the directory you prefer).
We then retrieve the value of the cookie “userDetails” (using the global variable $_COOKIE). We also use the isset(); function to find out if the cookie is set:
Note: The value of the cookie is automatically URLencoded, and automatically decoded when received (to prevent URLencoding, use setrawcookie(); instead).


$cookie_name = "userDetails";
$cookie_value = "Alan Smith";
setcookie($cookie_name, $cookie_value, time() + (86400 * 7), "/"); // 86400 = 1 day

// if the cookie is not set
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else { // cookie set
echo "Cookie '" . $cookie_name . "' is set!
";
echo "Value is: " . $_COOKIE[$cookie_name];
}

How to 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:


setcookie("cookieTest", "test", time() + 3600, '/');

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

How to delete a cookie

To delete a cookie, we use the setcookie() function and set the expiration date in the past:
An example is listed below.


// set the expiration date to one hour ago
setcookie("userDetails", "", time() - 3600);

How to modify a cookie

To modify a cookie, we just set the cookie using the setcookie(); function:


$cookie_name = "userDetails";
$cookie_value = "John Smith";
setcookie($cookie_name, $cookie_value, time() + (86400 * 7), "/");

if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!
";
echo "Value is: " . $_COOKIE[$cookie_name];
}

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 0
There are currently no comments.