setcookie() 函數用於設置 cookie。
注釋:setcookie() 函數必須位於 <html> 標簽之前。
語法
setcookie(name, value, expire, path, domain);
下面看實例
代碼如下 復制代碼
<?php
//注銷cookie
if(isset($_GET['out'])){
setcookie('us',"");
echo "<script>location.href='index.php'</script>";
}
//設置cookie
if(isset($_POST['sub'])){
setcookie("us",$_POST['user'],time()+3600);
echo "<script>location.href='index.php'</script>";
}
//輸出和注銷cookie
if($_COOKIE['us']){
echo $_COOKIE['us'];
echo "<a href='index.php?out=out'>注銷cookies</a>";
}
?>
<form method="post">
<input type="text" name="user">
<input type="password" name="pass">
<input type="submit" name="sub" value="提交">
</form>