兩種方式:1、使用“setcookie(cookiename,NULL)”語句將cookie的值設為空;2、使用“setcookie('cookiename','',time()-3600)”語句將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 ['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."; ?>
推薦學習:《PHP影片教學》
以上是php刪除cookie的兩種方式是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!