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 

A collection of 23 tips on understanding and managing cookies in PHP. Clear explanations and tutorial exercises are provided on setting and receiving cookies, creating and removing persistent cookies, specifying domain and path to restrict cookies, finding cookies in cookie files, cookie limitations. Topics included in this collections:

  1. What Is a Cookie?
  2. How To Send a Cookie to the Browser?
  3. How To Receive a Cookie from the Browser?
  4. How To Test Cookies on a Web Server?
  5. What Is a Persistent Cookie?
  6. How To Set a Persistent Cookie?
  7. How To Test Persistent Cookies?
  8. How To Remove a Cookie?
  9. What Are Domain and Path Attributes for Cookies?
  10. How To Specify Domain and Path for a Cookie?
  11. What Is the Common Mistake When Setting Path and Domain on Temporary Cookies?
  12. How Cookies Are Transported from Servers to Browsers?
  13. How To View Cookie Header Lines?
  14. How Cookies Are Transported from Browsers to Servers?
  15. Where Are the Persistent Cookies Stored on Your Computer?
  16. How To Delete Cookie Files on Your Computer?
  17. How View the Content of a Cookie File?
  18. How Does FireFox Manage Cookies?
  19. In Which Does File FireFox Store Persistent Cookies?
  20. How Many Cookies Can You Set?
  21. How Large Can a Single Cookie Be?
  22. How Are Cookies Encoded During Transportation?
  23. How Can Other Webmaster Steal Your Cookies?

What Is a Cookie?

A cookie is a small amount of information sent by a Web server to a web browser and then sent back unchanged by the browser each time it accesses that server. HTTP cookies are used for authenticating, tracking, and maintaining specific information about users, such as site preferences and the contents of their electronic shopping carts. The term "cookie" is derived from "magic cookie", a well-known concept in computing which inspired both the idea and the name of HTTP cookies.

A cookie consists of a cookie name and cookie value. For example, you can design a cookie with a name of "LoginName" and a value of "FYICenter".

How To Send a Cookie to the Browser?

If you want to sent a cookie to the browser when it comes to request your PHP page, you can use the setcookie( ) function. Note that you should call setcookie() function before any output statements. The following script shows you how to set cookies:

<?php
  setcookie("LoginName","FYICenter");
  setcookie("PreferredColor","Blue");
  print("2 cookies were delivered.\n");
?>

How To Receive a Cookie from the Browser?

If you know that a cookie has been sent to the browser when it was visiting the server previously, you can check the built-in $_COOKIE array, which contains all cookies that were sent by the server previously. The script below shows you how to pickup one cookie from the $_COOKIE and loop through all cookies in $_COOKIE:

<?php

  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");
  }
  print("All cookies received:\n");
  foreach ($_COOKIE as $name => $value) {
     print "  $name = $value\n";
  }
?>

How To Test Cookies on a Web Server?

If you want to test cookies with a browser, you need to run a Web server locally, or have access to a Web server remotely. Then you can copy the following PHP cookie test page, setting_receiving_cookies.php, to the Web server:

<?php
  setcookie("LoginName","FYICenter");
  setcookie("PreferredColor","Blue");
  print("<pre>\n");
  print("2 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");
?>

If you open this PHP page with a browser as http://localhost/setting_receiving_cookies.php, you will get:

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

"0 cookies received" is because there was no previous visit from this browser. But if you click the refresh button of your browser, you will get:

2 cookies were delivered.
Received a cookie named as LoginName: FYICenter
2 cookies received.
  LoginName = FYICenter
  PreferredColor = Blue

(Continued on next part...)

Part:   1   2  3  4  5  6  7 


Selected Developer Jobs:

More...