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

How to delete cookies in php

藏色散人
Release: 2023-03-04 10:40:02
Original
2361 people have browsed it

How to delete cookies in php: 1. Set the cookie value to empty, and the statement is "setcookie('cookiename', '') or setcookie(cookiename, NULL);"; 2. Expire the cookie The time is set to "past".

How to delete cookies in php

Recommended: "PHP Video Tutorial"

Two methods for deleting/clearing cookies in php

php knows that there are two ways to use cookies. The first method is to set the cookie value to empty. The second method is to directly set the cookie expiration time to the past. This article introduces php to you. For examples of deleting cookies through these two methods, friends in need can refer to them.

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 Set 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 [&#39;cookie_user&#39;] . "<br />";
echo "You have successfully logged out.";
?>
Copy after login

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 [&#39;cookie_user&#39;] . "<br />";
echo "You have successfully logged out.";
?>
Copy after login

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