Setting and playing around with cookies can be a fun and useful way to save data on a user’s hard drive. It can successfully store valuable information which may be helpful the next time they come to the site. Its fairly simple to set up, and even easier to read. To use it, you have to remember some guidelines. You have to put the cookie code before you print out any other HTML in your script. The cookie will not be evident on the page until its refreshed, or the user visits the page again (It is sent with the current page data)

Here’s the code to set a variable:

<?php
  setcookie ("loginName", "Jimbo");
?>

Cookie Expiration

Now, the next time someone visits this page, or any other PHP page that cookie variable will be available. However by default this cookie will expire when the user turns his browser off. To extend the time to expire, set in seconds as the next field. For example:

<?php
   setcookie ("loginName", "jimbo", time()+3600);
?>

This cookie will expire in 3600 seconds or 1 hour.

Time Conversion table

1 minute   –  60s
1 hour   –  3600s
1 day   –  86400s
1 week   –  604800s
1 fortnight   –  1209600s
1 month   –  2419200s
3 month   –  7257600s
1 year   –  29030400s

Reading Cookie Information

The cookies for the web domain your page is in will be automatically loaded into PHP. You can get at them via two arrays:

$HTTP_COOKIE_VARS[“loginName”] or $_COOKIE[“loginName”]

So to display the cookie data on screen all you need is:

<?php
  print $_COOKIE["loginName"]
?>

Practical Cookies : User Prefs

Following example shows a practical way to implement cookies to store user preferences.

<?php
if(!$_COOKIE["body_color"])
{
   setcookie("body_color", "#000000", time()+3600);
   setcookie("text_color", "#FFFF00", time()+3600);
 } ?>

text=<?php echo $_COOKIE["text_color"] ?>
>
Hello buddy

Multiple Cookies

It is not a problem to have multiple cookies – save it, here is a code example:
<?
   setcookie ("loginName", "jimbo");
   setcookie ("password", "bosh");
   setcookie ("hits", "3");

   print $_COOKIE["loginName"]."";
   print $_COOKIE["password"]."";
   print $_COOKIE["hits"]."";
?>

As such it is viewed as bad coding to use more than one cookie, and so people tend to store all variables they need in ONE cookie. This is easy in PHP because of the explode() and implode() commands.

Deleting Cookies

There are two ways of deleting cookies. The traditional way

<?
   setcookie ("cookie", "", time()-86400);  
?> 

Or simply by setting the cookie as nothing:

<?
   setcookie ("cookie");  
?>

Exploding Cookies

As I said before you can also use implode and explode.

$info[0] = "Jimbo";
$info[1] = "bosh";
$cookie = implode($info, "-");
setcookie ("myCookie", $cookie, time()+86400);

And you can take them out as follows

$cookie = $_COOKIE[‘myCookie'];
$info = explode($cookie, "-");

Of course you need to remember that element 0 of the info array is the username and element 1 is the password. But this way you can build up huge cookies.

Problems with Cookies

  • Not only are cookies painful to code.
  • It may seem a suprisingly low statistic, but Cookies are about 30% unreliable on the web right now and it’s getting worse.
  • More and more web browsers are starting to come with security and privacy settings and people browsing the net these days are starting to frown upon Cookies because they store information on their local computer that they do not want stored there.

So what is the best way of maintaining state?

use Sessions!

Next >> Lecture 8. Sessions in PHP

Tagged with: PHP
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.