Home  >  Article  >  Backend Development  >  What are the two ways to delete cookies in php

What are the two ways to delete cookies in php

青灯夜游
青灯夜游Original
2021-05-13 17:58:472284browse

Two ways: 1. Use the "setcookie(cookiename,NULL)" statement to set the cookie value to empty; 2. Use "setcookie('cookiename','',time()-3600)" statement sets the cookie's expiration time to the past.

What are the two ways to delete cookies in php

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

php delete/clear cookies One method

Set the cookie value to empty, that is: setcookie('cookiename', '') or setcookie(cookiename, NULL);

Set the cookie expiration time For the past, that is: setcookie('cookiename','',time()-3600);

Method 1: Set the cookie value 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.";
?>

Method 2: 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.";
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the two ways 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