Server Administration
  Home arrow Server Administration arrow Page 3 - HTTP State Management with Cookies
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SERVER ADMINISTRATION

HTTP State Management with Cookies
By: Bruce Coker
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-08-27

    Table of Contents:
  • HTTP State Management with Cookies
  • Cookie structure
  • Implementation
  • Cookies and PHP
  • Web Server Cookie Support

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    HTTP State Management with Cookies - Implementation


    (Page 3 of 5 )

    From a web development point of view, there are various methods by which cookies can be implemented. Broadly speaking, they can be implemented in a straightforward manner through common web development languages including JavaScript, PHP and ASP, all of which have functions for creating cookies and specifying their contents. Plain HTML cannot be used to implement cookies.

    Cookies and JavaScript

    Cookies can be manipulated in JavaScript using the document.cookie property. This property makes it relatively easy to create reusable functions for setting and retrieving them. A simple function to set a cookie containing just the required name=value pair might look something like this:


    function makecookie( name, value ) {

    var my_cookie = name + "=" + escape( value );

     

    document.cookie = my_cookie;


    }

     

    A slightly more complex function is needed to retrieve the cookie's information, since it is necessary to parse all the document's cookies for the one we want. To do this, the document.cookie property can be treated as a string. Either the search or match method will work, e.g.:


    function readcookie( name ) {


    var Name = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );

     

    if ( name )

    return ( unescape ( name[2] ) );

    else

    return null;

    }


    More complex cookies can be constructed by including additional parameters in the function declaration. In this example three parameters are used to specify the expiry date:


    function makecookie ( name, value, x-y, x-m, x-d) {


    var x_date = new Date ( x-y, x-m, x-d );

    var my_cookie = name + "=" + escape( value ) + "; expires=" + x_date.toGMTString();


    document.cookie = my_cookie;

    }


    The toGMTString method converts the supplied year, month and day to a format suitable for defining cookie expiration dates. You can use the expiry parameter to delete the cookie, by invoking the function and supplying the current date and time for expiry.

    More Server Administration Articles
    More By Bruce Coker


     

    SERVER ADMINISTRATION ARTICLES

    - Processes in the UNIX Shell
    - Migrating from Windows to Wine
    - Wine: Not Another Emulator
    - Preventive Measures to Block SSH Attacks
    - Monitoring Temperatures with Cacti
    - Cacti: RRDTool-based Graphing Solution
    - Network Magic 5.0 Review
    - Netfilter and Iptables Overview
    - Installing and Configuring Squid
    - Clickfree PC Backup Systems Compared
    - Squid, the Caching Proxy
    - Regular Expressions in the Unix Shell
    - Source Code Version Control Solutions
    - OTRS: Open Source Ticket Request System
    - Clonezilla: Free Mass Disk-Cloning Utility





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek