iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

August 7, 2008
Wish XML a happy birthday
About
 
August 7, 2008
Poll: How important is SEO to your overal website strategy?
About
 
August 7, 2008
How to Create a Search Feature with PHP and MySQL
WebReference.com
 
August 7, 2008
1 comment
.net
 
August 6, 2008
10 New SEO Reports
About
 
August 5, 2008
Array Creator
EarthWeb.com
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /Cookies and Sessions

Basics of Setting Cookies with PHP 

  Views:    9867
  Votes:    3
by Kirill Talanov 11/21/03 Rating: 

Synopsis:

Cookies are useful for many things: they can keep track of where you've been, what forms you have filled in and they can even keep you logged in into a website! With this 'yummy' tutorial you will learn how to create, read and delete cookies from a user's hard drive.
Pages: firstback1 forwardlast
The Article

Deleting Cookies and Common Mistakes

Deleting cookies is very easy (don't you just love the complexity of this all?): to delete a cookie, you must set a cookie with an expiration date in the past, and give that cookie an empty value.

No, we're not going to teleport the cookie back in time, we'll simply make the computer think that is already expired.
For example, this is the code to delete the "username" cookie that we set earlier:

setcookie ("username", "", time()-99999);

Note: The -99999 value can be any negative value you wish.

Common Cookie Mistakes

The most common mistake that people make is setting a cookie after there has been output, which is usually signified by an error similar to the following:

Warning: Cannot modify header information - headers already sent by (output started at /home/yoursite/public_html/setcookie.php:2) in /home/yoursite/public_html/setcookie.php on line 4

Take note that cookies are sent along with the page headers and therefore must be sent before any of the page is output! "Output" includes text, images and whitespace. This does not apply to reading a cookie, a cookie can be read from any point in the page.

Conclusion

You should now be able to do basic tasks involving cookies as well as be aware of their limitations.

Have fun!

Pages: firstback1 forwardlast

Similar/related articles:


 
  Sponsors