本系统,与之前在《asp 连接access数据库的登陆系统》()一文中的asp登录系统的思想完全一样,只是编程语言从asp变成了php,数据库从access变成了mysql。
一、基本目标
首先在mysql存在着如下的用户信息表:
在页面中有一个登录表单,上面需要用户填写用户名与密码等信息
如果用户输入的用户名在用户信息表里面根本就没有,那么则弹出“查无此人”的对话框,并返回本页
如果用户输入的密码错误,那么则弹出“密码错误”的对话框,并返回本页
如果用户输入的登录信息正确,那么则跳到“登录成功”的页面,并输出用户当前的ip、使用的系统的语言环境、与浏览器信息
“登录成功”的页面是被保护的,浏览器并不可以通过在浏览器中输入地址的方式,绕开输入密码的页面,直接访问“登录成功”页面
二、基本思想
用户输入用户名与密码的那一页用静态页面,只要其表单指向登录判断页login.php就可以了。
welcome.php是用户成功登录的页面,
exit.php用来销毁session。
session就是用来存放用户名与密码的浏览器全局变量。
三、制作过程
(1)login.html
没什么好说的,就一个登录表单,见如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" " http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd " > <html xmlns= " http://www.w3.org/1999/xhtml " > <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" /> <title>登录页面</title> </head> <body> <form action= "login.php" method= "post" > 用户名:<input type= "text" name= "username" /><br /> 密码:<input type= "password" name= "password" /><br /> <input type= "submit" value= "登录" /> </form> </body> </html> |
(2)exit.php
销毁session页面,并且在销毁session之后,把页面打回login.html
值得注意的是,在php一旦需要使用session,就应该在所有代码的第一行使用session_start();系统函数,否则可能会出错
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" " http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd " > <html xmlns= " http://www.w3.org/1999/xhtml " > <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" /> <title>登出页面</title> </head> <body> <?php session_start(); session_destroy(); ?> <script> window.location.href= "login.html" ; </script> </body> </html> |
(3)login.php
登录判断页面,还是经典的登录三段论,首先接收login.html传递过来的username与password,查询用户信息表中是否有这个username,如果没有,再登录失败,如果有,再同时判断传过来的、用户输入的password是否等于这个username在数据库中对应的dbpassword,如果是,登录成功,并把username存入session,传递给登录成功页面,否则登录失败。
本页面还用到了系统内置函数is_null判断查询结果是否为空,如果数据库查询结果为空,根本就不会有值赋予给新定义的dbusername,这个dbusername依旧为空。
同时,如果登录成功之后,还要使用mt_rand(0,100000);在0~100000中产生一个保护登录成功页面的随机数code。
并且做完一切判断之后,记得在最后加入一条关闭数据库的语句,人走带门。
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
|
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" " http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd " > <html xmlns= " http://www.w3.org/1999/xhtml " > <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" /> <title>登陆中……</title> </head> <body> <?php session_start(); $username = $_request [ "username" ]; $password = $_request [ "password" ]; $con =mysql_connect( "localhost" , "root" , "root" ); if (! $con ){ die ( "数据库连接失败!" ); } mysql_select_db( "test" , $con ); $dbusername =null; $dbpassword =null; $result =mysql_query( "select * from user where username='" . $username . "';" ); while ( $row =mysql_fetch_array( $result )){ $dbusername = $row [ "username" ]; $dbpassword = $row [ "password" ]; } if ( is_null ( $dbusername )){ ?> <script> alert( "查无此人!" ); window.location.href= "login.html" ; </script> <?php } else { if ( $dbpassword != $password ){ ?> <script> alert( "密码错误!" ); window.location.href= "login.html" ; </script> <?php } else { $_session [ "username" ]= $username ; $_session [ "code" ]=mt_rand(0,100000); ?> <script> window.location.href= "welcome.php" ; </script> <?php } } mysql_close( $con ); ?> </body> </html> |
(4)welcome.php
在welcome.php成功登录页面中,先要用isset来判断是否有login.php传递过来的code,这里的isset与上面is_null的区别,isset是判断有没有这个变量,is_null是在有这个变量的前提下,判断这个变量是否为null,如果没有code这个session则说明用户非正常途径登录的,马上扔到exit.php再见走人
如果是正常登录,则通过server对象里面的各个小对象输出用户信息。
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
|
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" /> <title>欢迎登录</title> </head> <body> <?php session_start(); if (isset( $_session [ "code" ])){ ?> 欢迎登录!<?php echo "${_session[" username "]}" ; ?><br /> 你的ip:<?php echo "${_server['remote_addr']}" ; ?> <br /> 你的系统语言:<?php echo "${_server['http_accept_language']}" ; ?> <br /> 你的浏览器为:<?php echo "${_server['http_user_agent']}" ; ?> <br /> <a href= "exit.php" >退出登录</a> <?php } else { ?> <script> alert( "请正常登录!" ); window.location.href= "exit.php" ; </script> <?php } ?> </body> </html> |
至此,整个登录系统开发完成。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。