How to delete cookies in php

青灯夜游
Release: 2023-03-13 07:40:02
Original
2356 people have browsed it

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;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);
    }
?>
Copy after login

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] => https://www.php.cn/
    [name] => PHP中文网
)
清除 url 的值
// 第三次运行
Array
(
    [name] => PHP中文网
)
Copy after login
Copy after login

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;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);
    }
?>
Copy after login

The running results are as follows:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template