网页登录中实现记住用户名和密码的功能
网页记住用户名,就是我们经常会用到的,登录下面有一个复选框,可以设置用户7天内或1个月不需要登录,只要你进行本网站系统查询cookie是否有相差用户名与密码如果是就把信息提取再到数据库中查询,如果cookie中的用户名与密码是一样的就实现用户自动登录了,PHP实例代码如下:
<?php
error_reporting(0);
session_start();
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>网页登录中实现记住用户名和密码的功能(完成自动登录)</title>
</head>
<body>
<?php
$uid = $_cookie['uid'];
$pwd = $_cookie['uid'];
if( $uid !='' && $pwd !='' )
{
//sql数据库查询验证
$_session['uid'] ='abc';
}
if( $_session['uid'] )
{
echo '会员中心,发表文章,到http://www.phprm.com去玩';
}
else
{
?>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="uid"></label>
<input type="text" name="uid" id="uid" />
</p>
<p>
<label for="pwd"></label>
<input type="text" name="pwd" id="pwd" />
</p>
<p>
<input type="checkbox" name="checkbox" id="checkbox" value="7" />
<label for="checkbox"></label>
一周内不用登录
</p>
<p>
<input type="submit" name="button" id="button" value="登录" />
</p>
<p> </p>
</form>
<?php
}
?>
</body>
</html>
<?php
if( $_post )
{
$info = $_post;
if( $info['uid'] !='' && $info['pwd'] !='')
{
//sql查询操作,用户名与密码到数据库中验证
if( intval($info['checkbox']) )
{
setcookie('uid',$info['uid'],time()+3600*24*7,'/','192.168.0.118');
setcookie('pwd',$info['pwd'],time()+3600*24*7,'/','192.168.0.118');
}
$_session['uid'] ==$info['uid'];
}
}
?>文章链接:http://www.phprm.com/develop/fs4316.html
随便收藏,请保留本文地址!