Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php删除cookie的两种方式是什么

php删除cookie的两种方式是什么

青灯夜游
青灯夜游asal
2021-05-13 17:58:472295semak imbas

两种方式:1、使用“setcookie(cookiename,NULL)”语句将cookie的值设置为空;2、使用“setcookie('cookiename','',time()-3600)”语句将cookie的过期时间设置为过去。

php删除cookie的两种方式是什么

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑

php删除/清除cookie的两种方法

将cookie的值设置为空,即:setcookie('cookiename', '')或者setcookie(cookiename, NULL);

将cookie的过期时间设置为过去,即:setcookie('cookiename','',time()-3600); 

方法一:将cookie的值设置为空

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

方法二:将cookie的过期时间设置为过去

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

推荐学习:《PHP视频教程

Atas ialah kandungan terperinci php删除cookie的两种方式是什么. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP如何安装XdebugArtikel seterusnya:php怎么将值强制转为字符串类型