Home  >  Article  >  Backend Development  >  What is the function to delete cookies in php

What is the function to delete cookies in php

青灯夜游
青灯夜游Original
2021-07-08 17:22:175117browse

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.

What is the function to delete cookies in php

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 &#39;<pre class="brush:php;toolbar:false">&#39;;
    if(!isset($_COOKIE[&#39;url&#39;]) && !isset($_COOKIE[&#39;name&#39;])){
        setcookie(&#39;url&#39;,&#39;https://www.php.cn&#39;);
        setcookie(&#39;name&#39;,&#39;php中文网&#39;);
        echo &#39;首次运行,设置 url、name 两个 Cookie 的值&#39;;
    }else if(isset($_COOKIE[&#39;url&#39;])){
        echo &#39;查看 Cookie 的值,如下所示:<br>&#39;;
        print_r($_COOKIE);
        echo &#39;清除 url 的值&#39;;
        setcookie(&#39;url&#39;,&#39;&#39;);
    }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 &#39;<pre class="brush:php;toolbar:false">&#39;;
    if(!isset($_COOKIE[&#39;url&#39;]) && !isset($_COOKIE[&#39;name&#39;])){
        setcookie(&#39;url&#39;,&#39;https://www.php.cn&#39;);
        setcookie(&#39;name&#39;,&#39;php中文网&#39;);
        echo &#39;首次运行,设置 url、name 两个 Cookie 的值&#39;;
    }else if(isset($_COOKIE[&#39;url&#39;])){
        echo &#39;查看 Cookie 的值,如下所示:<br>&#39;;
        print_r($_COOKIE);
        echo &#39;清除 url 的值&#39;;
        setcookie(&#39;url&#39;,&#39;https://www.php.cn&#39;, 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn