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