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

时间:2016-07-24
php清楚cookie有两种方法,第一种方法是将cookie的值设置为空,第二种方法是直接把cookie的过期时间设置为过去,本文章向大家介绍php通过这两种方法删除cookie的实例,需要的朋友可以参考一下。

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

  1. 将cookie的值设置为空,即:setcookie('cookiename', '')或者setcookie(cookiename, NULL);
  2. 将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 ['cookie_user'] . "<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 ['cookie_user'] . "<br />";
echo "You have successfully logged out.";
?>

上一页 下一页

原文地址:http://www.manongjc.com/article/1253.html