利用简单的表单提交验证PHP中cookie。Login.php中存放有用户信息。我们需要把这些信息保存到客服端cookie文件中。提交给本身页面进行验证。如果用户登录过,根据cookie中存放的信息验证用户是否登录。如果登陆过则cookie部位空。则把用户的cookie信息显示到表单中。cookie有效时间1个小时。
1:Login.php
1:Login.php
<?php
$error_msg = 'You are not submit,please submit<br/>';
if(isset($_POST['submit'])){
//cookie存放时间
setcookie('username',$_POST['username'],time()+3600);
setcookie('password',$_POST['password'],time()+3600);
}
echo '获取cookie,用户名:'.$_COOKIE['username'].'密码:'.$_COOKIE['password'].'<br/>';
if(isset($_COOKIE['username'])){
if(isset($_COOKIE['password'])){
echo 'wo got you have lanuched';
?>
<html>
<head>
<title>PHPcookie测试</title>
</head>
<body>
<p>PHPcookie验证测试</p>
<!-- $_SERVER['PHP_SELF']表示跳转到自身页面- -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
用户名:<input type="text" name="username"
value=<?php if(!empty($_COOKIE['username'])) echo $_COOKIE['username'];?>><br/>
密码:<input type="password" name="password"
value=<?php if(!empty($_COOKIE['password'])) echo $_COOKIE['password'];?>><br/>
<input type="submit" value="登陆" name="submit"/>
</form>
</body>
<?php
}
}else{
echo $error_msg;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="username"/><br/>
<input type="password" name="password"/><br/>
<input type="submit" value="登陆" name="submit"/>
</form>
<?php
}