Reply
Cookie how to in php
Old 09-24-2005, 11:01 AM Cookie how to in php
Skilled Talker

Posts: 60
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.

Code:
<?php 
setcookie("uname", "Gaetano", time()+36000); 
?>
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.

Code:
<?php
echo $_COOKIE[‘uname’]; //this will print Gaetano
?>
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.

Code:
<?php 
setcookie("uname", "", time() - 3600); 
?>
bambolin is offline
Reply With Quote
View Public Profile Visit bambolin's homepage!
 
When You Register, These Ads Go Away!
Old 09-25-2005, 10:24 PM
HitRaj47's Avatar
Extreme Talker

Posts: 177
Location: GA
How can you set a cookie to last until either the user clears the cookies in their browser, or until the site deletes the cookie?

THanks.
HitRaj47 is offline
Reply With Quote
View Public Profile
 
Old 09-28-2005, 03:23 PM
Skilled Talker

Posts: 60
The user can delete the cookie any time he / she wants to. In order to delete a cookie. If you want to delete a cookie on the user computer then use:

PHP Code:
<?php 
setcookie
("uname"""time() - 3600); 
?>
bambolin is offline
Reply With Quote
View Public Profile Visit bambolin's homepage!
 
Old 09-28-2005, 03:25 PM
Skilled Talker

Posts: 60
and if you meant that you want a cookie that never expires to my best knowledge and believes you cannot do that but you can always give a very large number say 2 years and update that value everytime the user logs againt for instance! :-)
bambolin is offline
Reply With Quote
View Public Profile Visit bambolin's homepage!
 
Old 09-28-2005, 05:19 PM
HitRaj47's Avatar
Extreme Talker

Posts: 177
Location: GA
Ah, I see. I'll do the second suggestion, setting the expiration time sometime in the future.

Thanks for your help!
HitRaj47 is offline
Reply With Quote
View Public Profile
 
Old 09-28-2005, 10:24 PM
stOx's Avatar
Machine

Latest Blog Post:
Worlds Smallest Car - Peel P50
Posts: 2,114
Name: Matt. (>',')>
Location: London, England.
A google cookie dosn't expire untill 2038

As far as i know theres no reason to delete a cookie unless its an option you want to give people, but then it dosnt actually delete the cookie, its just sets one with a short expiry time or one that contains no data.

I usually set mine to time() +60*60*24*7*365 (a year)
stOx is offline
Reply With Quote
View Public Profile Visit stOx's homepage!
 
Old 10-01-2005, 08:23 PM
Skilled Talker

Posts: 60
Exactly it is a little trick that you do in order to go round the problem :-)
bambolin is offline
Reply With Quote
View Public Profile Visit bambolin's homepage!
 
Reply     « Reply to Cookie how to in php
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13727 seconds with 12 queries