Cookies  
Cookie

A "cookie" is a name/value pair (example: username = zimmer) saved on a client's hard disk.

name is username

value is zimmer

A "cookie" text file is created based on the web server and the directory from which the cookie is set.

A single web server can create up to 20 cookies on a client's hard disk

A cookie is retrievable by the name (ex: username) and only by the web server that created  it.

The client's web browser saves the cookie - client's can block this via internet settings

Session cookies - cookies with no expiration date (disappear when session ends).

Persistent cookies - cookies with an expiration data in the future.

Cookies can be created, retrieved, and deleted.
 

Javascript
cookies

document.cookie can be used to access the cookie...

Create a session cookie:

        onclick = "document.cookie = 'username = zimmer'; "
 

Create a persistent cookie:

     onclick = "document.cookie =
         'username=zimmer ;expires=Sat, 19-Dec-2009 00:00:00 GMT';"


Retrieve a cookie:

        onclick = "document.writeln(document.cookie);"


Delete a cookie:

     onclick = "document.cookie =
        'username=zimmer ;expires=Sat, 17-Oct-2009 00:00:00 GMT';"

** Many functions that can assist cookie processing using JavaScript :

WM: Advanced JS & Cookies

 
php
cookies

setcookie("name","value", ...); 
                        // can be used to access a cookie in a PHP script
                        // must be the first thing sent to the client's browser
                        // (even before <html> tag!)

Create a session cookie:

    <? php
                 setcookie("username","zimmer");

    ?>
 

Create a persistent cookie:

    <? php
                 setcookie("username","zimmer", time( ) + (60*60*24*3));
                                         // 3 days from now it will expire

    ?>

 

Retrieve a cookie:

    - just use the cookie name portion as a variable

    <? php

         print("<br> your cookie value is ");
         print( $_COOKIE["username"] );
         print ("<br>");

     ?>

    - the above script sends the following back to the client's browser:

            <br>your cookie value is zimmer <br>

 

Delete a cookie:

    <? php

            setcookie("username");   
                   // just using the name has the effect of deleting
                   // the cookie named username

    ?>

OR

    <? php

            setcookie("username", "", time()-60);   
                    // sets expiration date to the past

    ?>

 

 

Example Form for creating cookies

PHP code used by sample form for CGI processed cookies