网页HTML登录注册PHP+MySQL实现

简介

数据库原理课程过于枯燥无味。。于是就想做点什么。就做一个用数据库登录和注册的玩意吧。
涉及知识有以下几点,前三个不是重点,主要是数据库方面:

  1. HTML表单
  2. CSS
  3. PHP
  4. SQL
    验证码的部分不用深究,这里只是为了装,由于简要设计,还未考虑SQL注入、密码加密

效果预览

登录和注册页面可以相互跳转,登录成功后成功后显示“登陆成功”,失败则重新输入
网页HTML登录注册PHP+MySQL实现_第1张图片
网页HTML登录注册PHP+MySQL实现_第2张图片

代码实现

  1. 数据库表的实现
--  userforP 的数据库结构
DROP DATABASE IF EXISTS `userforP`;
CREATE DATABASE IF NOT EXISTS `userforP` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;
USE `userforP`;

--   表 userforP.userforP 结构
DROP TABLE IF EXISTS `userforP`;
CREATE TABLE IF NOT EXISTS `userforP` (
  `Uname` int(11) NOT NULL COMMENT '用户名',
  `Upassword` int(11) NOT NULL COMMENT '密码',
  `Ulogtimes` int(10) unsigned zerofill NOT NULL COMMENT '密码错误次数',
  PRIMARY KEY (`Uname`,`Upassword`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  1. 登录数据库验证
 
/**
* 登录验证
*/

$dbhost = "localhost";
$charset = 'utf8';
$dbname = "userforP";	//数据库名称
$dbuser = "Admin2";		//数据库用户名
$dbpass = "123456789";	//数据库密码
$tbname = 'userforP'; 	//表格名
$name=$_POST['name'];  
$password=$_POST['password'];  

try
{
	$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=$charset", $dbuser, $dbpass);
	$sql = "SELECT * FROM userforP where Uname='$name'and Upassword='$password'";
	
	if ( $query = $conn->query($sql) ) 
	{
		if($query->rowCount()<1)	//如果数据库中找不到对应数据
		{
			echo"";  
		}
		else
		{	
	//增加密码错误后错误次数和锁定功能
	//登录成功后错误次数归零,跳转到对应页面
			$reset="UPDATE userforP.userforP SET Ulogtimes=0 WHERE Uname = '$name'";
			$stmt = $conn->prepare($reset);
			$count = $stmt->execute();//执行预处理语句
			echo"";  
		}
	}
	else
	{
		echo "Query failed\n";
	}
	$conn = null; // 关闭连接
}
catch(PDOException $e)
{
	echo $e->getMessage();
}

?>
  1. 注册-插入数据库
 
/**
* 注册信息验证
*/
session_start();
if($_SESSION["validcode"] != $_POST['codeNum'])
{
	echo"";  
}
else
{
	$user=$_POST['user'];
	$pass=$_POST['pass'];
	$confirm=$_POST['confirm'];
	$dbhost = "localhost";
	$charset = 'utf8';
	$dbname = "userforP";	//数据库名称
	$dbuser = "Admin2";		//数据库用户名
	$dbpass = "123456789";	//数据库密码
	$tbname = 'userforP'; 	//表格名
	if($pass!=$confirm)
	{    
		echo "";
	}
	else
	{	
		try
		{
			$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=$charset", $dbuser, $dbpass);
			$sql = "INSERT INTO userforP.userforP(Uname,Upassword) VALUES(?,?)";
			$stmt = $conn->prepare($sql);			
			$stmt->bindValue(1,$user);//绑定参数
			$stmt->bindValue(2,$pass);
			$count = $stmt->execute();//执行预处理语句
			if($count<>0)
			{
				echo"";  
			}
			else
			{
				echo"";  
			}
			$stmt = null;//释放
			$conn = null; // 关闭连接
		}
		catch(PDOException $e)
		{
			echo $e->getMessage();	
		}
	}
}	
?>

  1. 登录页面
    sign.php

session_start();
header("Content-Type:text/html;charset=utf-8");
?>
<html>
	<head>
		<meta  charset="utf-8">
		<title>登录</title>
		<link href="C.css" rel="stylesheet" type="text/css" />
	</head>
 <body>
		<div class="box"> 
		<h2>登录</h2>
		<form action="check.php" method="post" enctype="multipart/form-data">
			<div class="inputBox">
				<input type="text" name="name" value="" required="required" placeholder=
					"           请输入您的学号" pattern="[0-9]{13}" title="学号为13为数字"><label>学号</label></div>
			<div class="inputBox">
				<input type="password" name="password" value=""	required="required" placeholder=
					"           请输入您的密码"><label>密码</label></div>
					
			<input type="submit" name="submit" value="登录">
			<input type="button" onclick="window.location.href='register.php'" name="submit" value="注册" >			
					
		</form>
		</div>
	</body>
</html>

  1. 注册页面

header("Content-Type:text/html;charset=utf-8");
?>
<html>
	<head>
		<meta charset="utf-8">
		<title>注册</title>
		<link href="C.css" rel="stylesheet" type="text/css" />
	</head>
 <body>
		<div class="box"> 
		<h2>注册</h2>
		<!--将用户输入的user,和pass提交到login.php-->
		<form action="checkregister.php" method="post" enctype="multipart/form-data">
			<div class="inputBox"><input type="text" name="user" value="" required="required"
				placeholder=   "            请输入您的学号" pattern="[0-9]{13}" title="学号为13为数字"><label>学号</label></div>
			<div class="inputBox"><input type="password" name="pass" value=""required="required"
				placeholder="            请输入您的密码"><label>密码</label></div>
			<div class="inputBox"><input type="password" name="confirm" value=""required="required"
				placeholder="            请重复您的密码"><label>确认密码</label></div>
			<div class="inputBox"><input type="text" value="" name="codeNum" required="required"
				placeholder="            请输入验证码" pattern="[0-9]{4}"  title="验证码为4个数字"><label>验证码</label></div>
			<input type="submit" name="submit" value="确认注册">
			<input type="button" onclick="window.location.href='sign.php'" value="返回登陆">
			<img src="vericode.php" style="width:100px;height:25px;" id="code"/>
		</form>
		</div>
	</body>
</html>


总结

完整代码请到Github上免费下载调戏:https://github.com/BackMountainDevil/Web/tree/master
CSDN因为狗币玩意不放了。

参考

  • PHP中文网
  • 数据库概论-王姗
  • https://blog.csdn.net/Misaka_0/article/details/79313294

你可能感兴趣的:(SQL,mysql,数据库,php)