The function to delete cookies in php is "setcookie()". The deletion method: 1. Use the setcookie() function to set the cookie value to empty; 2. Use the setcookie() function to expire the cookie. Just set the time to be less than the current time of the system.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php function to delete cookies-- -setcookie()
When a cookie is created, if its expiration time is not set, its cookie file will be automatically deleted when the browser is closed. If you want to delete the cookie file before closing the browser , you also need to use the setcookie() function.
Deleting Cookies is similar to creating Cookies. You only need to use the setcookie() function to set the Cookie value (that is, the second parameter) to empty, or set the Cookie expiration time (that is, the third parameter). Parameter) can be set smaller than the current time of the system.
Example 1 of clearing cookies: Use the setcookie() function to set the value of the cookie to empty
<?php echo '<pre class="brush:php;toolbar:false">'; if(!isset($_COOKIE['url']) && !isset($_COOKIE['name'])){ setcookie('url','https://www.php.cn'); setcookie('name','php中文网'); echo '首次运行,设置 url、name 两个 Cookie 的值'; }else if(isset($_COOKIE['url'])){ echo '查看 Cookie 的值,如下所示:<br>'; print_r($_COOKIE); echo '清除 url 的值'; setcookie('url',''); }else{ print_r($_COOKIE); } ?>
Output:
// 第一次运行 首次运行,设置 url、name 两个 Cookie 的值 // 第二次运行 查看 Cookie 的值,如下所示: Array ( [url] => https://www.php.cn [name] => php中文网 ) 清除 url 的值 // 第三次运行 Array ( [name] => C语言中文网 )
Clear Cookie Example 2: By setting the Cookie expiration time (that is, the third parameter) to be less than the current time of the system
<?php echo '<pre class="brush:php;toolbar:false">'; if(!isset($_COOKIE['url']) && !isset($_COOKIE['name'])){ setcookie('url','https://www.php.cn'); setcookie('name','php中文网'); echo '首次运行,设置 url、name 两个 Cookie 的值'; }else if(isset($_COOKIE['url'])){ echo '查看 Cookie 的值,如下所示:<br>'; print_r($_COOKIE); echo '清除 url 的值'; setcookie('url','https://www.php.cn', time()-1); }else{ print_r($_COOKIE); } ?>
Output:
// 第一次运行 首次运行,设置 url、name 两个 Cookie 的值 // 第二次运行 查看 Cookie 的值,如下所示: Array ( [url] => https://www.php.cn [name] => php中文网 ) 清除 url 的值 // 第三次运行 Array ( [name] => php中文网 )
Recommended learning:《PHP video tutorial》
The above is the detailed content of What is the function to delete cookies in php. For more information, please follow other related articles on the PHP Chinese website!