PHP实现简单登陆注册页面

PS:个人学习记录

数据库设置:

login.html:

<!DOCTYPE html>
<html>
	<head>
		<meta charset = "utf-8">
		<title>用户登录</title>
	</head>
	
	<body>
		<form action = "login.php" method = "post">
			<p>用户名:<input type = "text" name = "username"></p> 
			<p>密码:<input type = "text" name = "password"></p>
			<p><input type = "submit" value = "登陆" name = "subl"></p>
			
			<a href = "register.html">没有账号,立即注册</a>
		</form>
	</body>
</html>

login.php:


<?php
	header("Content-Type: text/html;charset=utf-8");
	//建立连接
	$conn = mysqli_connect('localhost','root','root');
	if($conn){
		//数据库连接成功
		$select = mysqli_select_db($conn,"user_login");		//选择数据库
		if($select){
			//数据库选择成功
			if(isset($_POST["subl"])){
				
				$user = $_POST["username"];
				$pass = $_POST["password"];
				if($user == ""||$pass == ""){
					//用户名or密码为空
					//弹窗提示信息并返回登陆页面
					echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."用户名或密码不能为空!"."\"".")".";"."</script>";
					echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."login.html"."\""."</script>";
					exit;
				}
				//sql语句
				$sql_select = "select username,password from users where username = '$user' and password = '$pass'";
				//设置编码
				mysqli_query($conn,'SET NAMES UTF8');
				//执行sql语句
				$ret = mysqli_query($conn,$sql_select);
				$row = mysqli_fetch_array($ret); 
				
				//用户密码正确
				if($user == $row['username']&&$pass == $row['password']){
					//跳转登陆成功页面
					 header("Location:loginsucc.php");
				}else{
					//跳转登陆失败页面
					header("Location:loginfal.php");
				}  
			}
		}
		//关闭数据库
		mysqli_close($conn);
	}else{		
		//连接错误处理
		die('Could not connect:'.mysql_error());
	}	

?>

loginsucc.php:

<!DOCTYPE html>
<html>
	<head>
		<title>登录成功</title>
		<meta name="content-type"; charset=UTF-8">
	</head>
	<body>
		<div>
            <h1>登录成功!</h1>
		</div>
	</body>
</html>

loginfal.php:

<!DOCTYPE html>
<html>
	<head>
		<title>登录失败</title>
		<meta name="content-type"; charset=UTF-8">
	</head>
	<body>
		<div>
            <h1>登录失败!</h1>
			<a href="login.html">重新登陆</a> 
		</div>
	</body>
</html>

register.html:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>注册用户</title>
	</head>
	
	<body>
		<form action = "register.php" method = "post">
			<p>用户名:<input type="text" name="username"></p> 
			<p>密码:<input type="text" name="password"></p>
			<p>确认密码:<input type = "text" name = "re_password"></p>
			<p><input type="submit" value="立即注册" name="subr"></p>
</form>
</body>
</html>

register.php:

<?php
	header("Content-Type: text/html;charset=utf-8");
	//建立连接
	$conn = mysqli_connect('localhost','root','root');
	if($conn){
		//数据库连接成功
		$select = mysqli_select_db($conn,"user_login");		//选择数据库
		if(isset($_POST["subr"])){
			
			$user = $_POST["username"];
			$pass = $_POST["password"];
			$re_pass = $_POST["re_password"];
			
			if($user == ""||$pass == ""){
				//用户名or密码为空
				echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."用户名或密码不能为空!"."\"".")".";"."</script>";
				echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."register.html"."\""."</script>";
				exit;
			}
			if($pass == $re_pass){
				//两次密码输入一致
				mysqli_set_charset($conn,'utf8');	//设置编码
				
				//sql语句
				$sql_select = "select username from users where username = '$user'";
				//sql语句执行
				$result = mysqli_query($conn,$sql_select);
				//判断用户名是否已存在
				$num = mysqli_num_rows($result); 
				
				if($num){
					//用户名已存在
					echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."用户名已存在!"."\"".")".";"."</script>";
					echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."register.html"."\""."</script>";
					exit;
				}else{
					//用户名不存在
					$sql_insert = "insert into users(username,password) values('$user','$pass')";
					//插入数据
					$ret = mysqli_query($conn,$sql_insert);
					$row = mysqli_fetch_array($ret); 
					//跳转注册成功页面
					header("Location:registersucc.php");
				}
			}else{
				//两次密码输入不一致
				echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."两次密码输入不一致!"."\"".")".";"."</script>";
				echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."register.html"."\""."</script>";
				exit;
			}
		}
		//关闭数据库
		mysqli_close($conn);
	}else{
		//连接错误处理
		die('Could not connect:'.mysql_error());
	}

?>

registersucc.php:

<!DOCTYPE html>
<html>
	<head>
		<title>注册成功</title>
		<meta name="content-type"; charset=UTF-8">
	</head>
	<body>
		<div>
            <h1>注册成功!</h1>
			<a href="login.html">立即登陆</a> 
		</div>
	</body>
</html>

 

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值