cookie 常用於識別用戶。cookie 是一種伺服器留在用戶計算機上的小檔案。每當同一台計算機通過瀏覽器請求頁面時,這台計算機將會傳送 cookie。通過 PHP,您能夠創建並取回 cookie 的值。
如何創建 Cookie?
語法
setcookie(name,value,expire,path,domain);
實例
<?phpsetcookie("user","AlexPorter",time()+3600);?><html>.....
如何取回 Cookie 的值?
在下面的實例中,我們取回了名為 "user" 的 cookie 的值,並把它顯示在了頁面上:
<?php//Printacookieecho$_COOKIE["user"];//Awaytoviewallcookiesprint_r($_COOKIE);?>
<html><body><?phpif(isset($_COOKIE["user"]))echo"Welcome".$_COOKIE["user"]."!<br>";elseecho"Welcomeguest!<br>";?></body></html>
如何刪除 Cookie?
<?php//settheexpirationdatetoonehouragosetcookie("user","",time()-3600);?>