Home > Article > Backend Development > How to delete cookies in php
How to delete cookies in php: 1. Use the "setcookie('cookiename', '')" method to set the cookie value to empty; 2. Use the "setcookie('cookiename', '', time( )-3600);" method.
Recommendation: "PHP Video Tutorial"
Two ways to delete/clear cookies in php
Set the cookie value to empty, that is: setcookie('cookiename', '') or setcookie(cookiename, NULL);
Set the cookie expiration time to the past, that is: setcookie ('cookiename','',time()-3600);
Method 1: Set the cookie value to empty
<?php setcookie ( "cookie_user", "test", time () + 60 * 60 * 24 * 30 ); setcookie ( "cookie_pass", md5 ( "test" ), time () + 60 * 60 * 24 * 30 ); function logout() { setcookie ( "cookie_user", "", time () + 60 * 60 * 24 * 30 ); setcookie ( "cookie_pass", "", time () + 60 * 60 * 24 * 30 ); } /* http://www.manongjc.com/article/1253.html */ logout (); echo $_COOKIE ['cookie_user'] . "<br />"; echo "You have successfully logged out."; ?>
Second method: Set the cookie expiration time to the past
<?php setcookie ( "cookie_user", "test", time () + 60 * 60 * 24 * 30 ); setcookie ( "cookie_pass", md5 ( "test" ), time () + 60 * 60 * 24 * 30 ); function logout() { setcookie ( "cookie_user", "test", time () - 100 ); setcookie ( "cookie_pass", md5 ( "test" ), time () - 100 ); } logout (); echo $_COOKIE ['cookie_user'] . "<br />"; echo "You have successfully logged out."; ?>
The above is the detailed content of How to delete cookies in php. For more information, please follow other related articles on the PHP Chinese website!