Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - Understanding and Managing Cookies

By: FYICenter.com

Part:   1  2   3  4  5  6  7 

(Continued from previous part...)

What Is a Persistent Cookie?

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.

How To Set a Persistent Cookie?

If you want to set a persistent cookie, you can use the setcookie() function with an extra parameter to specify its expiration time. To follow sample script sets 2 persistent cookies to be expired within 7 days:

  setcookie("LoginName","FYICenter");
  setcookie("PreferredColor","Blue");
  setcookie("CouponNumber","07470433",time()+60*60*24*7);
  setcookie("CouponValue","100.00",time()+60*60*24*7);
  print("2 temporary cookies were delivered.\n");
  print("2 consistent cookies were delivered.\n");

How To Test Persistent Cookies?

If you want to test persistent cookies, you can copy the following PHP script, setting_persistent_cookies.php, to your Web server:

<?php
  setcookie("LoginName","FYICenter");
  setcookie("PreferredColor","Blue");
  setcookie("CouponNumber","07470433",time()+60*60*24*7);
  setcookie("CouponValue","100.00",time()+60*60*24*7);
 
  print("<pre>\n");
  print("2 temporary cookies were delivered.\n");
  print("2 consistent cookies were delivered.\n");

  if (isset($_COOKIE["LoginName"])) {
    $loginName = $_COOKIE["LoginName"];
    print("Received a cookie named as LoginName: ".$loginName."\n");
  } else {
    print("Did not received any cookie named as LoginName.\n");
  }
 
  $count = count($_COOKIE);
  print("$count cookies received.\n");
  foreach ($_COOKIE as $name => $value) {
     print "  $name = $value\n";
  }
  print("</pre>\n");
?>

Open your browser to visit this page: http://localhost/setting_persistent_cookies.php. You will see:

2 temporary cookies were delivered.
2 consistent cookies were delivered.
Did not received any cookie named as LoginName.
0 cookies received.

Click the refresh button, you will see:

2 temporary cookies were delivered.
2 consistent cookies were delivered.
Received a cookie named as LoginName: FYICenter
4 cookies received.
  LoginName = FYICenter
  PreferredColor = Blue
  CouponNumber = 07470433
  CouponValue = 100.00

Close your browser and open it again to the same page. You will see:

2 temporary cookies were delivered.
2 consistent cookies were delivered.
Did not received any cookie named as LoginName.
2 cookies received.
  CouponNumber = 07470433
  CouponValue = 100.00

This proves that "CouponNumber" and CouponValue" persisted outside the browser.

(Continued on next part...)

Part:   1  2   3  4  5  6  7 


Selected Developer Jobs:

More...