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...)

How To View Cookie Header Lines?

If you are interested to see the cookie header lines, or you are having trouble with your cookies and need to see the cookies to help debugging, you can run your script with PHP CGI interface in a command line window. The following tutorial exercise shows you a good example:

>edit showing_cookie_header_lines.php
<?php
  setcookie("LoginName","FYICenter");
  setcookie("PreferredColor","Blue", NULL, "/store");
  setcookie("CouponNumber","07470433",time()+60*60*24*7,"/store");
  setcookie("CouponValue","100.00",time()+60*60*24*7,
    "/store", ".fyicenter.com");
  print("4 cookies were delivered.\n");
?>

>php-cgi showing_cookie_header_lines.php
Content-type: text/html
X-Powered-By: PHP/5.0.4
Set-Cookie: LoginName=FYICenter
Set-Cookie: PreferredColor=Blue; path=/store
Set-Cookie: CouponNumber=07470433; expires=Sun, 05 Mar 2006
  02:33:43 GMT; path=/store
Set-Cookie: CouponValue=100.00; expires=Sun, 05 Mar 2006
  02:33:43 GMT; path=/store; domain=.fyicenter.com

4 cookies were delivered.

How Cookies Are Transported from Browsers to Servers?

Cookies are transported from a Web browser to a Web server in the header area of the HTTP request message. Each cookie will be included in a separate "Cookie:" header line in the following format:

GET / HTTP/1.1
Cookie: name1=value1
Cookie: name2=value2
Cookie: name3=value3
......
Accept: */*

Where Are the Persistent Cookies Stored on Your Computer?

The location and file names where persistent cookies are stored on your computer depend on which browser you are using. If you using Microsoft Internet Explorer, persistent cookies are stored in the \Documents and Settings\$user\Cookies directory. Cookies are stored in multiple cookie files with one file per Web server. Check your cookie directory on your local system, you will be surprised to see how many Web servers are setting persistent cookies to your computer.

How To Delete Cookie Files on Your Computer?

A simple way to delete cookie files on your computer is to use the function offered by the IE browser. The following tutorial exercise shows you how to delete cookie files created by IE:

  • Open IE (Internet Explorer)
  • Go to Options/Internet Options
  • Click the Delete Cookies button on the options dialog window.

Check the cookie directory again. All cookie files should be deleted.

How View the Content of a Cookie File?

Cookie files are normal text files. You can view them with any text editor. Follow the steps below to see what is in a cookie file created by your own PHP script.

Copy the following sample 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");
?>

(Continued on next part...)

Part:   1  2  3  4   5  6  7 


Selected Developer Jobs:

More...