1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Cookies - HOWTO

Discussion in 'PHP' started by bamb0lin, Sep 23, 2005.

  1. #1
    Cookies are a very useful mechanism to remember information. HTTP protocol is stateless protocol that is, once a page is requested by the client and served by the server, no information is kept. That is where cookies come into the scene. Cookies allow you to store information on the user’s hard disk.

    If you need more information about cookies in general visit http://www.cookiecentral.com/faq/

    When dealing with cookies in PHP you have to remember three things:

    1. Setting and deleting a cookie before any output to the web browser is sent.
    2. In order to access a cookie information you must refresh the page
    3. A cookie can only be access either from the page it has set it or from all the pages in the sub folder from where the cookie has been set. More on this later.



    Setting a cookie in PHP

    Setting a cookie in PHP is very simple. Just use the setcookie command before any output to the web browser is sent.

    <?php 
    setcookie("uname", "Gaetano", time()+36000); 
    ?>
    PHP:
    In this example three a cookie called uname is being set up. The value of uname will be “Gaetano”. The other parameter sets the value of the expiration date. Using the function time(), you get the current UNIX time stamp. I have added 36000 seconds to the UNIX time stamp so that the expiration time of the uname cookie will be of 10 hours.

    Accessing a cookie

    Accessing a cookie in PHP is very simple. You can get the cookie value using the $_COOKIE[] associative array. The key to use is the cookie name. The example below illustrates how to do this.

    <?php
    echo $_COOKIE[‘uname’]; //this will print Gaetano
    ?>
    PHP:
    Deleting a cookie

    In order to delete a cookie, use the setcookie function to set the expiry date to an hour ago. The cookie will be automatically deleted from the user’s hard disk.

    <?php 
    setcookie("uname", "", time() - 3600); 
    ?>
    PHP:

    Conclusion

    More snippets and example can be found at http://www.snippetcollection.com Feel free to join our community at http://www.snippetcollection.com/forums/.

    THE END
     
    bamb0lin, Sep 23, 2005 IP
    digitalpoint likes this.
  2. Connect

    Connect Guest

    Messages:
    191
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nice tips there.
     
    Connect, Sep 23, 2005 IP
  3. bamb0lin

    bamb0lin Well-Known Member

    Messages:
    401
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #3
    thanks for your comments :) More tips like these to come! ;-)
     
    bamb0lin, Sep 23, 2005 IP
  4. rederick

    rederick Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How would I set a cookie that never expires? ...I have
    setcookie("registered",$cookie_data,time()+60*60*24*30);
    PHP:
    This sets it for 30 days...

    Thanks
     
    rederick, Sep 27, 2005 IP
  5. bamb0lin

    bamb0lin Well-Known Member

    Messages:
    401
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #5
    hmmm strictly speaking you cannot set it "never expires" BUT you can set it to a value thatit seems like never expiring. For instance set the expiry date for 2 years and everytime the user logs in refresh the expiry date. That is the way how I go around this problem! :)
     
    bamb0lin, Oct 1, 2005 IP
  6. obrusoft

    obrusoft Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks , I've new know about cookies using
     
    obrusoft, Oct 7, 2005 IP
  7. Dread

    Dread Peon

    Messages:
    323
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Google actually sets a cookie that lasts until 17th January 2038. Although this cookie does expire its most likely to outlive the computer its been stored on, providing of course the user doesnt delete the cookie.

    So feel free to set the cookie to however long you like, try setting it: 60*60*24*365*10 which is 10 years.

    You can read more about the google cookie from an anti-google site, although im not anti google myself it does make very good reading with some very valid points.
     
    Dread, Oct 7, 2005 IP
  8. bamb0lin

    bamb0lin Well-Known Member

    Messages:
    401
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #8
    very good example. The cookie system does not allow to create a cookie for ever but you can just create a cookie to live of a long time enough to make sure the user wiill be dead by that time :p
     
    bamb0lin, Oct 7, 2005 IP