简介:
效果:通过前端页面的注册,通过MD5将密码加密,发送到数据库,通过验证数据库的内容实现登录,以及各种保证安全的措施
实验环境:php+html+css+mysql数据表,使用html +css设计注册,以及用户登陆表以及注册表
我们将创建以下文件
1,conn.php //连接数据库
2,login.php登录的后端处理
3,login.html登录前端表
4,loginok.php用户登录完成后的主页面
5,regist.html //用户注册的前端页面
6,regist_ok.php用户注册后端处理文
7,创建数据表 crerate table register(account varchar(20) not null primary kry,
password varchar(50) not null
实验开始
1,建立数据库连接-conn.php
<?php
$servername = "localhost";*
$usename = "root"; *
$passwd = "202019";*
$dbname="lv"; &//标注*号的部分需要根据实际情况进行更换
$conn = new mysqli($servername, $usename, $passwd,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}?>
2,建立用户登录前端页面-login.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<style type="text/css">
#aaa{width: 300px;
height: 30px;}
body{background-image:url('MTXX_MH20240504_162234715.jpg');
background-size: cover;
}
</style><body>
<center>
<h2>登录界面</h2>
<form method="POST" action="loginok.php"> //表格提交位置
账号 :<input id="aaa" type="text" name="uname" placeholder="用户名" />
<br /><br/>
密码 :<input id="aaa"type="password" name="pwd" placeholder="密码" />
<br />
<input type="submit" >
<input type="reset"> <a href="regist.php">注册账号</a>
</form>
</center>
</body>
</html>
效果展示
登陆时
登陆后展示
3,用户登录后端loginok.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>welcome</title>
</head>
<style>body {background-image:url('MTXX_MH20240504_162234715.jpg');
background-size:cover;
background-filter:blur(5px); //背景素材放在评论区了</style>
<body><?php
header("content-type:text/html; charest=UTF-8");
session_start();
if(isset($_POST['uname']) && isset($_POST['pwd'])){
$name =filter_var($_POST['uname'],FILTER_SANITIZE_ENCODED);
$pwd = $_POST['pwd'];
$hashedpwd=md5($pwd); //MD5加密
require("conn.php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM register WHERE (account='$name') AND (password='$hashedpwd')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo"<br>";
echo '<nav class="top-bar"><ul><li><a href ="rly.html">'.$row['account'].'</a>
</li>
<li><a href="#">'.$row['password'].'</a>
</li>
</ul>
</nav>';
}
} else {
echo "没有您要的信息";
}
$conn->close();
}
?>
</body>
</html>
4,用户注册前端 regist.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>登录欢迎页</title>
</head>
<style>
body {background-image:url('MTXX_MH20240504_162234715.jpg');
background-size:cover;
background-filter:blur(5px);
}<style rtpe="text/css">
.aaa{width:300px;
height:30px;}
</style>
<style type="text/javascripts"></style>
</style>
<body>
<?php
?>
<center>
<h2>注册界面</h2>
<form method="POST" name="from1" action="regist_ok.php">
姓名: <input class="aaa" type="text" name="username"><br/><br/>
密码: <input class="aaa" type="text" name="password"><br/><br/>
<input type="reset"name="reset" value="重置">
<input type="submit"name="submit" value="注册" onClick="myfunction">
<a href="login.php"><< 返回上一页</a>
<a href="loginok.php">点击注册</a>
</form>
</body>
</html>
效果展示
注册前端
注册后端
5,注册后端 regist_ok.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>regist_ok.php</title>
</head>
<style>body {background-image:url('MTXX_MH20240504_162234715.jpg');
background-size:cover;
background-filter:blur(5px);</style>
<body><?php
header("content-type:text/html; charest=UTF-8");
session_start();
if(isset($_POST['uname']) && isset($_POST['pwd'])){
$name =filter_var($_POST['uname'],FILTER_SANITIZE_ENCODED);//清楚特殊字符防止注入
$pwd = $_POST['pwd'];
$hashedpwd=md5($pwd); //MD5加密
require("conn.php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM register WHERE (account='$name') AND (password='$hashedpwd')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo"<br>";
echo '<nav class="top-bar"><ul><li><a href ="rly.html">'.$row['account'].'</a>
</li>
<li><a href="#">'.$row['password'].'</a>
</li>
</ul>
</nav>';
}
} else {
echo "没有您要的信息";
}
$conn->close();
}
?>
</body>
</html>
在本次本次实验中,我们将密码加密发送至数据库,保护数据的安全,由于MD5的单项加密过程,使得我们在每次输入同样的数字带来的相同的加密内容,使得在保证用户交互的情况下,保证密码安全,同时在防止sql植入中,我们设计加入了filter函数对特殊字符进行转义,总之,这只是一次很小的进步,师兄,路还长!