您的位置:首页 > 其它

04-使用cookie实现记录用户名称的需求

2013-08-16 20:31 429 查看
login.php

<?php
require_once 'common.php';
?>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="content-type">
</head>
<img alt="logo.gif" src="./images/logo.gif">
<hr />
<h1>管理员登录系统</h1>
<form action="loginProcess.php" method="post">
<table>
<tr>
<td>用户id</td>
<td><input type="text" name="id"
value="<?php
echo getCookieVal ( 'id' )?>" /></td>
</tr>
<tr>
<td>密  码</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="keep" value="yes" />是否保存用户ID</td>
</tr>
<tr>
<td><input type="submit" value="用户登录" /></td>
<td><input type="reset" value="重新填写" /></td>
</tr>
</table>
</form>
<?php
header ( "content-type:text/html;charset=utf-8" );
if (! empty ( $_GET ['errno'] )) {
$errno = $_GET ['errno'];
if ($errno == 1) {
echo "<font color='red' size='3'>你的用户名或密码错误</font>";
}
}
?>
<hr />
<img alt="foot.jpg" src="./images/foot.jpg">
</html>


loginProcess.php
//3.获取用户是否选中了保存id
if(empty($_POST['keep'])){
if(!empty($_COOKIE['id']))
//删除cookie
setcookie("id",$id,time()-100);
}else{
//设置cookie
setcookie("id",$id,time()+7*2*24*3600);
}

common.php(获取cookie的方法)
function getCookieVal($key) {
if (empty ( $_COOKIE [$key] )) {
return "";
} else {
return $_COOKIE [$key];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: