Rumah  >  Artikel  >  php教程  >  网页登录中实现记住用户名和密码的功能

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

WBOY
WBOYasal
2016-05-25 16:42:461467semak imbas

网页记住用户名,就是我们经常会用到的,登录下面有一个复选框,可以设置用户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[&#39;uid&#39;]; 
    $pwd = $_cookie[&#39;uid&#39;]; 
    if( $uid !=&#39;&#39;  && $pwd !=&#39;&#39; ) 
    { 
     //sql数据库查询验证 
     $_session[&#39;uid&#39;] =&#39;abc&#39;; 
    } 
    if( $_session[&#39;uid&#39;] ) 
    { 
     echo &#39;会员中心,发表文章,到http://www.phprm.com去玩&#39;; 
    } 
    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[&#39;uid&#39;] !=&#39;&#39; && $info[&#39;pwd&#39;] !=&#39;&#39;) 
     { 
      //sql查询操作,用户名与密码到数据库中验证 
       
      if( intval($info[&#39;checkbox&#39;]) ) 
      { 
       setcookie(&#39;uid&#39;,$info[&#39;uid&#39;],time()+3600*24*7,&#39;/&#39;,&#39;192.168.0.118&#39;); 
       setcookie(&#39;pwd&#39;,$info[&#39;pwd&#39;],time()+3600*24*7,&#39;/&#39;,&#39;192.168.0.118&#39;);    
      } 
      $_session[&#39;uid&#39;] ==$info[&#39;uid&#39;]; 
     } 
    } 
?>


文章链接:

随便收藏,请保留本文地址!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php 用户登录代码 Artikel seterusnya:PHP 与 js json的通信实例