How to Delete Cookies in PHP

How to Delete Cookies in PHP thumbnail
How to Delete Cookies in PHP

Deleting an existing cookie from a client machine requires resetting the expiration date. You can in effect set the cookie to nothing and then remove it from the client machine. The best way to do this is to set the expiration date to be some time in the past.

Things You'll Need

  • Access to web server where the PHP code will be stored
Show More

Instructions

    • 1

      Use the mktime() command to create a date in the past and set it to a variable. In this example, January 1, 1970 will be used to better circumvent a problematic discrepancy between the actual date and the date of the client machine.

      $pastdate = mktime(0,0,0,1,1,1970);

    • 2

      Use the setcookie() command to reset the cookie. The first parameter is the name of the cookie that is previously determined, the second command is the value (here an empty string), and the third parameter is the expiration date (here the previously set $pastdate variable).

      setcookie("CookieName","",$pastdate);

    • 3

      Finish your script and clean up.

Tips & Warnings

  • There is a possibility that the client machine's system date and time may be set incorrectly and in such a way that the expiration date may not be earlier than the web server's system date. In this case, the cookie will merely be set to an empty string rather than deleted.

Related Searches:

References

Resources

  • Photo Credit Kate Schneider

Comments

You May Also Like

  • How to Delete Cookies Using PHP

    PHP, or Hypertext Preprocessor, is an open-source scripting language primarily used for web programming. PHP code can be embedded into normal HTML...

  • How to Remove Cookies Using PHP

    A cookie is a text file placed in the cache of a browser. Its purpose is to store information that the browser...

  • How to Delete Cookies

    Cookies are text files that are automatically downloaded to your hard drive by Web sites to make Web browsing easier. If you...

  • How to Delete a File Using PHP

    The PHP unlink function is used to delete files. The function is called "unlink" because when a file is deleted, it is...

  • How to Delete History and Cookies in Windows Vista

    Sometimes when your computer is running slow it means that you need to clean out the history, files, and unnecessary cookies in...

  • How to Delete Tracking Cookies in Windows XP

    There are several different ways to delete tracking cookies in Windows XP. These cookies should be deleted frequently, as they could cause...

  • How to Delete Cookies

    Delete cookies from an Internet browser by opening the "Tools" menu, clicking on "Internet Options," selecting "Browsing History" and then choosing to...

  • How to Remove Session Cookies

    Cookies are pieces of information that allow a computer to access webpages faster the next time you visit them. If cookies are...

  • How to Delete Cookies and Clean Your Computer

    If you notice your Internet browser acting sluggish or your computer performance dropping, it's likely due for a cleanup. Windows 7 includes...

  • How to Start and Clear a Session in PHP

    PHP developers use sessions as a way of creating sets of data that can pass from page to page within a website....

  • How to Reset Cookies on the Computer

    The term "cookies" in relation to your Web browser refers to the small bits of data that are collected and stored as...

  • A Tutorial on Using HTML Cookies

    A cookie is a small file used by a Web browser to save session data. For example, on a shopping website, shopping...

  • How to Set a Session Variable in PHP

    PHP is a Web programming language used in many sites to run database-driven sites and e-commerce sites. One of the many attributes...

  • How to Remove Cache in PHP

    When programming in PHP, there are certain instances that you may not want information to be cached, as it may affect data...

  • How to Delete a PHP File

    PHP is an open source scripting language. It is often used on web servers as a way to make dynamic web pages....

  • How Do I Empty the Cookie Cache?

    Cookies are special text files stored on your computer by your browser and websites to store information between visits. Used properly, they...

  • How to Delete Cookies

    Cookies can be deleted from Internet Explorer using the Properties tab, control panel or Tools window. Remove cookies from a computer desktop...

  • How to Clear Your Cookies

    Cookies are text files that Web sites store on your computer to recall your preferences and personal settings whenever you return to...

Related Ads

Featured