当前位置:首页 > PHP教程 > php应用 > 列表

网页登录中实现记住用户名和密码的功能

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-16 22:34:25 浏览: 评论:0 

网页记住用户名,就是我们经常会用到的,登录下面有一个复选框,可以设置用户7天内或1个月不需要登录,只要你进行本网站系统查询cookie是否有相差用户名与密码如果是就把信息提取再到数据库中查询,如果cookie中的用户名与密码是一样的就实现用户自动登录了,PHP实例代码如下:

  1. <?php 
  2. error_reporting(0); 
  3. session_start(); 
  4. ?> 
  5. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
  6. <html xmlns="http://www.phpfensi.com/1999/xhtml"
  7. <head> 
  8. <meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
  9. <title>网页登录中实现记住用户名和密码的功能(完成自动登录)</title> 
  10. </head> 
  11. <body> 
  12. <? 
  13. $uid = $_cookie['uid']; 
  14. $pwd = $_cookie['uid']; 
  15. if$uid !=''  && $pwd !='' ) 
  16.  //sql数据库查询验证 
  17.  $_session['uid'] ='abc'
  18. if$_session['uid'] ) 
  19.  echo '会员中心,发表文章,到http://www.phpfensi.com去玩'
  20. else 
  21. ?> 
  22. <form id="form1" name="form1" method="post" action=""
  23.   <p> 
  24.     <label for="uid"></label> 
  25.     <input type="text" name="uid" id="uid" /> 
  26.   </p> 
  27.   <p> 
  28.     <label for="pwd"></label> 
  29.     <input type="text" name="pwd" id="pwd" /> 
  30.   </p> 
  31.   <p> 
  32.     <input type="checkbox" name="checkbox" id="checkbox" value="7" /> 
  33.     <label for="checkbox"></label> 
  34.     一周内不用登录 
  35.   </p> 
  36.   <p> 
  37.     <input type="submit" name="button" id="button" value="登录" /> 
  38.   </p> 
  39.   <p>&nbsp;</p> 
  40. </form> 
  41. <? 
  42. ?> 
  43. </body> 
  44. </html> 
  45.  
  46. <?php 
  47. if$_post ) 
  48.  $info = $_post
  49.  if$info['uid'] !='' && $info['pwd'] !=''
  50.  { 
  51.   //sql查询操作,用户名与密码到数据库中验证 
  52.    
  53.   ifintval($info['checkbox']) ) 
  54.   { 
  55.    setcookie('uid',$info['uid'],time()+3600*24*7,'/','192.168.0.118'); 
  56.    setcookie('pwd',$info['pwd'],time()+3600*24*7,'/','192.168.0.118');    
  57.   } 
  58.   $_session['uid'] ==$info['uid']; 
  59.  } 
  60. ?> 

Tags: 记住用户名 记住密码 功能

分享到: