demo

head.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<ul>
			<li>
				<a href="login.php">登录</a>
			</li>
			<li>
				<a href="register.php">注册</a>
			</li>
		</ul>
	</body>
</html>

login.php


	require_once("head.html");
?>
<form action="check.php" method="post">
	<label for="">用户名</label>
	<input type="text" name="username" id="username" />
	<br />
	<label for="">密码</label>
	<input type="password" name="password" id="password" value="" />
	<br />
	<input type="submit" value="登录" />
</form>

register.php


	require_once("head.html");
?>
<form action="checkre.php" method="post">
	<label for="">用户名</label>
	<input type="text" name="username" id="username" />
	<br />
	<label for="">密码</label>
	<input type="password" name="password" id="password" value="" />
	<br />
	<input type="submit" value="注册" />
</form>

success.php


	session_start();
//	echo ($_SESSION['user']);
	if(isset($_SESSION['user'])==false){
		header("Location: head.html");
	}
?>
<h2>登录成功</h2>
您好,
	echo $_SESSION['user'];
?>

fail.php

<h2>登录失败</h2>
<p><a href="login.php">稍后即将返回,若无反应请点击此处</a></p>
<script>
	setInterval(
		function(){
			window.location.href = "login.php";
		}
	,3000);	
</script>

check.php


	require_once("dbop.php");
?>

	if(isset($_POST)==false){
		echo "信息未提供";
		header("Location:fail.php");
		exit();
	}
	$name = $_POST['username'];
	$pass = $_POST['password'];
	$sql="SELECT * FROM `admin` WHERE `username` ='{$name}'";
	$ans=query($sql);
//	var_dump($ans);
	if($ans->num_rows<=0){
		echo"用户名不存在";
		header("Location:fail.php");
		exit();
	}
	$row=$ans->fetch_array();
	if($row['password']!=$pass){
		echo"密码错误";
		header("Location:fail.php");
		exit();
	}
	session_start();
	$_SESSION['user']=$name;
	header("Location:success.php");
?>

checkre.php(待完善)


	require_once("dbop.php");
?>

	if(isset($_POST)==false){
		echo "注册失败:没有输入";
		header("Location:register.php");
		exit();
	}
	$name = $_POST['username'];
	$pass = $_POST['password'];
	$sql="SELECT * FROM `admin` WHERE `username` ='{$name}'";
	$ans=query($sql);
	if($ans->num_rows>0){
		echo"注册失败:用户名已存在";
		header("Location:register.php");
		exit();
	}
	$sql="INSERT INTO `admin` ( `username`, `password`) VALUES ($name, $pass)";
	query($sql);
	header("Location:login.php");
?>

dbop.php


	function query($sql){
		$db=new mysqli("localhost","root","","dome");
		$db->query("set names utf8");
		$ret = $db->query($sql);
		$db->close();
		return $ret;
	}
?>

你可能感兴趣的:(php)