使用静态网页语言html和服务器端脚本语言PHP实现登陆功能;
浏览器通过POST方法与服务器进行请求-响应(HTTP方法:GET-从指定的资源请求数据;POST-向指定的资源提交要被处理的数据);
服务器获取表单提交的数据,并对数据库用户表进行查询,如果能成功匹配,则允许用户进行登陆,否则不能登录。
数据库
根目录
访问地址
http://localhost/login/login.php
页面
login.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<form method="POST" action="login_check.php">
<table border="1" align="center"width="420" height="370">
<tr align="center">
<td valign="center">
<table border="0" width="400" height="350">
<tr>
<td colspan="2"><img src="login_top.jpg"/></td>
</tr>
<tr height="40">
<td width="140" align="right" style="font-size:12px">用户名:</td>
<td><input name="uname" type="text" /></td>
</tr>
<tr height="40">
<td width="140" align="right" style="font-size:12px">密码:</td>
<td><input name="pwd" type="password" /></td>
</tr>
<tr height="40">
<td colspan="2" align="center"><input type="submit" value="登录" /></td>
</tr>
<tr height="40">
<td colspan="2" align="center" style="font-size:10px" bgcolor="#51A37C">By:Liu 2015.9.28</td>
</tr>
</table>
</td>
</tr>
</table>>
</form>
</body>
</html>
</pre><span style="color:#009900;">login_check.php</span><p></p><p><pre name="code" class="php"><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<?php
header("content-type:text/html;charset=utf-8");
//连接数据库
$dblink=mysql_connect("localhost","root","root") or die("数据库连接失败");
//设置字符串编码
mysql_query("set names utf8");
//选择数据库
mysql_select_db("user");
//获取表单数据。
$uname=$_POST['uname'];
$pwd=$_POST['pwd'];
//可以对密码进行md5加密。
//$pwd=md5($pwd);
$sql="select * from user where username='{$uname}'";
$rs=mysql_query($sql);<span style="white-space:pre"> </span>//执行sql查询
$num=mysql_num_rows($rs);<span style="white-space:pre"> </span>//获取记录数
if($num)<span style="white-space:pre"> </span>//用户存在;
{
<span style="white-space:pre"> </span>$row=mysql_fetch_array($rs);
<span style="white-space:pre"> </span>if($pwd===$row['password'])<span style="white-space:pre"> </span>//密码正确。
{
<span style="white-space:pre"> </span>echo "登陆成功,正在为你跳转至后台页面";
<span style="white-space:pre"> </span>//header("location:index.html");
}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>echo "密码不正确";
<span style="white-space:pre"> </span>echo "<a href='login.php'>返回登陆页面</a>";
<span style="white-space:pre"> </span>}
}
else
{
<span style="white-space:pre"> </span>echo '用户不存在';
<span style="white-space:pre"> </span>echo "<a href='login.php'>返回登陆页面</a>";
}
?>
</body>
</html>