Home / PHP/MySQL Tutorials / Archive by category 'PHP Cookies'

PHP Cookies

php-cookies

Storing Multiple Values in One Cookie using PHP

<?php
$info = array("xyz", "yellow", 22);
setcookie("usr", serialize($info));
?>
<html><head><title>Multiple Cookie Values</title></head>
<h2>$_COOKIE[]</h2>
<b>
<?php
if(! empty($_COOKIE['usr'])){
$cookie_data= $_COOKIE['usr'];
$cookie_data=stripslashes($cookie_data);
$cookie_data=unserialize("$cookie_data");
echo "What's in the cookie array< br />";
print_r($_COOKIE);
echo "<pre>Unserialized data< br />";
print_r( $cookie_data);
}
?>
</b>
</font>
</body>

Understanding persistent cookies

A persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer. By default, cookies are created as temporary cookies which stored only in the browser’s memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:

  • Temporary cookies can not be used for tracking long-term information.
  • Persistent cookies can be used for tracking long-term information.
  • Temporary cookies are safer because no programs other than the browser can access them.
  • Persistent cookies are less secure because users can open cookie files see the cookie values.

Cookies in PHP : Part 4 – Deleting a cookie

There is no function to actually delete a cookie. A cookie is deleted by invalidating it. A cookie is deleted using the setcookie() function by setting the expire time to a past value.

Example:

1
2
3
<?php
setcookie("user", "", time()-3600);
?>

In the above example, the expire time is being set to an hour ago. The cookie will be deleted as soon as the browser is started.


Cookies in PHP : Part 3 – Retrieving a cookie

The $_COOKIE associative array is used to retrieve the value of a cookie from user’s computer. This is a ‘superglobal’, or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

Example code:

1
2
3
<?php
// Print the value of a cookie
echo $_COOKIE["user"]; ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Example code:
 
<html>
<body>
 
<?php
if (isset($_COOKIE["user"]))
  echo "Welcome " . $_COOKIE["user"] . "!<br />";
else
  echo "Welcome guest!<br />";
?>
 
</body>
</html>

In the above code, the isset() function is used to check whether the cookie value is available on the user’s computer. If it is available, then we know that this is a returning user, otherwise a new user.


Cookies in PHP : Part 2 – Creating a cookie

The setcookie() function is used to create a cookie. The seetcookie() function should be called before the tag in the page.

Syntax:

setcookie(name, value, expire);

name specifies the name of the cookie. This name is later used to retrieve the cookie. The argument value specifies the value that needs to be stored on the user’s computer and the expire argument specifies the time when the cookie will expire. After the expiry time, the cookie will be deleted by the browser when it is restarted.

Example code:

1
2
3
4
5
6
7
<?php
$expire=time()+60*60*24*60;
setcookie("user", "w3mentor", $expire);
?>
 
<html>
.....

In the above example the name of the cokie is user and it stores the value w3mentor. The expire time is two months from the current time specified in seconds. The expire time is decoded as 60 seconds * 60 minutes * 24 hours * 60 days.


Cookies in PHP : Part 1 – Introduction

A cookie is a small text file containing name-values fields, stored by a web program on the user’s computer. The basic purpose of using a cookie is to identify the user. Each time a user requests a web page from the same computer, the cookie stored on the computer is also sent to the server. A cookie can be used to contain information which will later help the web program in authentication, remembering last session’s state, user preferences or any other data frequently used by the program.

PHP provides built-in functions which can be used for both creating and retrieving cookies.