What is Cookie?

A cookie is often used to identify a user.Cookies are text files stored on the client computer and they are kept of use tracking purpose. 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.

PHP transparently supports HTTP cookies.

There are three steps involved in identifying returning users −

  • Server script sends a set of cookies to the browser. For example name, age, or identification number etc.
  • Browser stores this information on local machine for future use.
  • When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user.

Create Cookies With PHP

A cookie is created with the setcookie() function.

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

Only the name parameter is required. All other parameters are optional.

Name − This sets the name of the cookie and is stored in an environment variable called HTTP_COOKIE_VARS. This variable is used while accessing cookies.

  • Value − This sets the value of the named variable and is the content that you actually want to store.
  • Expiry − This specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970. After this time cookie will become inaccessible. If this parameter is not set then cookie will automatically expire when the Web Browser is closed.
  • Path − This specifies the directories for which the cookie is valid. A single forward slash character permits the cookie to be valid for all directories.
  • Domain − This can be used to specify the domain name in very large domains and must contain at least two periods to be valid. All cookies are only valid for the host and domain which created them.
  • Security − This can be set to 1 to specify that the cookie should only be sent by secure transmission using HTTPS otherwise set to 0 which mean cookie can be sent by regular HTTP.

PHP Create/Retrieve a Cookie

The following example creates a cookie named “user” with the value “John Doe”. The cookie will expire after 30 days (86400 * 30). The “/” means that the cookie is available in entire website (otherwise, select the directory you prefer).

We then retrieve the value of the cookie “user” (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set:

Example

<?php

$cookie_name = “website_name”;

$cookie_value = “www.webtechsource.com”;

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), “/”); // 86400 = 1 day

?>

<html>

<body>

 

<?php

if(!isset($_COOKIE[$cookie_name])) {

echo “Website Name ‘” . $cookie_name . “‘ is not set!”;

} else {

echo “Website Name  ‘” . $cookie_name . “‘ is set!<br>”;

echo “Value is: ” . $_COOKIE[$cookie_name];

}

?>

</body>

</html>

Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).

Modify a Cookie Value

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

Example

<?php

$cookie_name = “website_name”;

$cookie_value = “http://www.webtechsource.com”;

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), “/”);

?>

<html>

<body>

<?php

if(!isset($_COOKIE[$cookie_name])) {

echo “website ame ‘” . $cookie_name . “‘ is not set!”;

} else {

echo “website name ‘” . $cookie_name . “‘ is set!<br>”;

echo “Value is: ” . $_COOKIE[$cookie_name];

}

?>

</body>

</html>

Delete a Cookie

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

Example

<?php

// set the expiration date to one hour ago

setcookie(“website_name”, “”, time() – 3600);

?>

<html>

<body>

 

<?php

echo “Cookie website_name is deleted.”;

?>

 

</body>

</html>

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:

Example

<?php

setcookie(“website_name”, “http://www.webtechsource.com”, time() + 3600, ‘/’);

?>

<html>

<body>

 

<?php

if(count($_COOKIE) > 0) {

echo “Cookies are enabled.”;

} else {

echo “Cookies are disabled.”;

}

?>

</body>

</html>

Read about session click here

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 45 = 54

%d bloggers like this: