php+MySql实现登录系统与输出浏览者信息功能

本系统,与之前在《ASP 连接Access数据库的登陆系统》(点击打开链接)一文中的asp登录系统的思想完全一样,只是编程语言从asp变成了php,数据库从Access变成了mysql。

一、基本目标

首先在mysql存在着如下的用户信息表:


在页面中有一个登录表单,上面需要用户填写用户名与密码等信息

如果用户输入的用户名在用户信息表里面根本就没有,那么则弹出“查无此人”的对话框,并返回本页


如果用户输入的密码错误,那么则弹出“密码错误”的对话框,并返回本页


如果用户输入的登录信息正确,那么则跳到“登录成功”的页面,并输出用户当前的IP、使用的系统的语言环境、与浏览器信息


“登录成功”的页面是被保护的,浏览器并不可以通过在浏览器中输入地址的方式,绕开输入密码的页面,直接访问“登录成功”页面


二、基本思想


用户输入用户名与密码的那一页用静态页面,只要其表单指向登录判断页login.php就可以了。

welcome.php是用户成功登录的页面,

exit.php用来销毁session。

session就是用来存放用户名与密码的浏览器全局变量。

三、制作过程
 (1)login.html 
没什么好说的,就一个登录表单,见如下代码: 

<!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" /><br /> 密码:<input type="password" /><br /> <input type="submit" value="登录" /> </form> </body> </html>

(2)exit.php

销毁session页面,并且在销毁session之后,把页面打回login.html
值得注意的是,在php一旦需要使用session,就应该在所有代码的第一行使用session_start();系统函数,否则可能会出错

<!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。
并且做完一切判断之后,记得在最后加入一条关闭数据库的语句,人走带门。

<!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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/de9e9aeee9b7a80d6d892f239b3854bd.html