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

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

WBOY
Release: 2016-05-25 16:42:46
Original
1512 people have browsed it

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

<?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;];

     }

    }

?>

Copy after login


文章链接:

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template