cookie使用案例
Posted hai-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cookie使用案例相关的知识,希望对你有一定的参考价值。
index.php
<html>
<body>
<h1>Login In</h1>
<?php
$user=isset($_COOKIE[‘id‘])?$_COOKIE[‘id‘]:‘‘;
$pwd=isset($_COOKIE[‘password‘])?$_COOKIE[‘password‘]:‘‘;
?>
<form method = "post" action="./base.php">
<table>
<tr>
<td>User id</td>
<td> <input type="text" name="id" value="<?php echo $user;?>"></td>
</tr>
<tr>
<td>Password</td>
<td ><input type="password" name="password" value="<?php echo $pwd;?>"></td>
</tr>
<tr>
<td><input type="submit" value="login"></td>
<td><input type="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>
base.php
<?php
$user=$_POST["id"];
$pwd=$_POST["password"];
if(!empty($_COOKIE[‘id‘])){
setcookie("id",$user,time()+3600);
}else{
setcookie("id",$user,time()+3600);
}
if(!empty($_COOKIE[‘password‘])){
setcookie("password",$user,time()+3600);
}else{
setcookie("password",$user,time()+3600);
}
echo "welcome, ".$_POST["id"]."login success!";
echo "<br/><a href=‘index.php‘>Baack to login</a>";
if(!empty($_COOKIE[‘lastVisit‘])){
echo "your last login time : ".$_COOKIE[‘lastVisit‘];
setcookie("lastVisit",date("Y-m-d H:i:s"),time()+24*3600*30);
}else{
echo "your first login time :";
setcookie("lastVisit",date("Y-m-d H:i:s"),time()+24*3600*30);
}
?>
以上是关于cookie使用案例的主要内容,如果未能解决你的问题,请参考以下文章