先写一套不含样式的html注册界面:
<html>
<head>
<meta charset="utf-8">
<title>注册</title>
</head>
<body>
<form action="register.php" method="post">
<h1>用户注册</h1>
<label>用户名:</label>
<input type="text" name="username">
<label>密码:</label>
<input type="password" name="userPwd">
<label>确认密码:</label>
<input type="password" name="reuserPwd">
<label>编号:</label>
<input type="text" name="code">
<input type="submit" value="register">
</form>
</body>
</html>
然后写PHP,命名为register.php:
<?php
header('Content-Type: text/html; charset=utf-8');
$username1 = $_POST['username'];
$userPwd1 = $_POST['userPwd'];
$reuserPwd1 = $_POST['reuserPwd'];
$code1 = $_POST['code'];
//!!接下来四个变量填写自己的数据库信息:
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
//连接数据库:
$conn = new mysqli($servername, $username, $password, $dbname);
//检测连接:
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$searchName = "select * from user where username='$username1'";
$searchNameResult = $conn->query($searchName);
$row = $searchNameResult->fetch_row();
$sql="insert into user values(null,'".$username1."','".$userPwd1."','".$code1."')";
if ($username1 == "" or $userPwd1 == ""){
echo '<script>alert("账号或密码不能留空");history.go(-1);</script>';
}
else if ($userPwd1 != $reuserPwd1){
echo '<script>alert("你两次输入的密码不一致");history.go(-1);</script>';
}
else if ($row > 0){
echo '<script>alert("已有人使用该账户名,请更换一个名字");history.go(-1);</script>';
}
else if ($conn->query($sql) === TRUE) {
echo '<script>alert("用户注册成功");</script>';
header("Refresh:0;url=index.html");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
接着我们再写一套html登录界面:
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<form action="login.php" method="post">
<h1>用户登陆</h1>
<label>用户名:</label>
<input type="text" name="username">
<label>密码:</label>
<input type="password" name="userPwd">
<input type="submit" value="login">
</form>
</body>
</html>
然后写PHP,命名为login.php:
<?php
header('Content-Type: text/html; charset=utf-8');
$username1 = $_POST['username'];
$userPwd = $_POST['userPwd'];
//2、
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
//!!接下来四个变量填写自己的数据库信息:
$conn = new mysqli($servername,$username,$password,$dbname);
//检测连接:
if ($conn->connect_error){
die("连接失败".$conn->connect_error);
}
//检查数据库:
$sql = "select userPwd from user where username='".$username1."'";
$allSql = "select * from user where username='".$username1."'";
$result = $conn->query($sql);
$allResult = $conn->query($allSql);
if ($username1 == "" or $userPwd == ""){
echo '<script>alert("账号或密码不能留空");history.go(-1);</script>';
}
else if($result->num_rows > 0){
$row = $result->fetch_row();
$db_userpwd = $row[0];
if($db_userpwd==$userPwd){
echo '欢迎用户'.$username1.'登录';
}else{
echo '<script>alert("账号或密码错误"); history.go(-1);</script>';
}
}else {
echo '<script>alert("用户不存在"); history.go(-1);</script>';
}
$conn->close();
?>
只要稍稍加入css和js样式与代码,就可以达到这样的效果(随着时间而变化的登录注册界面):界面部分信息敏感,已打马赛克。
(2021/9/16更新)
(更新(背景会无限缓缓移动))