Home >Backend Development >PHP Problem >How to delete cookies in php

How to delete cookies in php

青灯夜游
青灯夜游Original
2021-09-30 15:33:112398browse

In PHP, you can use the setcookie() function to delete cookies. You only need to set the second parameter of the function to empty, or set the third parameter to be less than the current time of the system.

How to delete cookies in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

When the cookie is created, if it is not set expiration time, the cookie file will be automatically deleted when the browser is closed. If you want to delete the cookie file before closing the browser, you need to use the setcookie() function.

There are two ways to delete cookies using the setcookie() function. Let me give you a detailed introduction below.

Method 1: Use the setcookie() function to set the Cookie value (that is, the second parameter) 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;//m.sbmmt.com/&#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);
    }
?>

Run the above code for the first time Two cookies named url and name will be created; run it again to view the value of the cookie and clear the value of url; run it a third time to view the cleared results. As shown below:

// 第一次运行
首次运行,设置 url、name 两个 Cookie 的值
// 第二次运行
查看 Cookie 的值,如下所示:
Array
(
    [url] => //m.sbmmt.com/
    [name] => PHP中文网
)
清除 url 的值
// 第三次运行
Array
(
    [name] => PHP中文网
)

Method 2: Use the setcookie() function to set 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;//m.sbmmt.com/&#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;//m.sbmmt.com/&#39;, time()-1);
    }else{
        print_r($_COOKIE);
    }
?>

The running results are as follows:

// 第一次运行
首次运行,设置 url、name 两个 Cookie 的值
// 第二次运行
查看 Cookie 的值,如下所示:
Array
(
    [url] => //m.sbmmt.com/
    [name] => PHP中文网
)
清除 url 的值
// 第三次运行
Array
(
    [name] => PHP中文网
)

Recommended learning: "PHP Video Tutorial"

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!

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