<html>
<!--登陆界面代码-->
<head>
<meta charset="utf8"/>
</head>
<div class="img">
<img src="./img/land.png" style="margin : 0 auto;"/>
</div>
<br/>
<form action='land.php' method="POST">
账号: <input type="text" name ='account' value=""/>
<br/>
<br/>
密码: <input type="password" name="password" />
<br/>
<br/>
<input type="submit" name="submit" value="登陆"/>
<input type="button" name="rigester" value="注册" onclick="window.location.href='register.html'"/>
</form>
</html>
<?php
//实现php 登陆功能
//并利用cookie技术实现七天免登录
$account = $_POST['account'] or die('账号提取失败');
$password = $_POST['password'] or die ('密码提取失败');
if(empty($account)||empty($password)){
echo ('账号密码不能为空');
header('Refresh:3,url=land.html');
exit;
}
//操作数据库
function meetError(){
echo mysqli_error($conn);
header('Refresh:3,url=land.html');
exit;
}
$conn = mysqli_connect('localhost','root',980613) or die('数据库连接失败');
$sql = 'set names utf8';
mysqli_query($conn,$sql) or meetError();
$sql = 'use register';
mysqli_query($conn,$sql) or meetError();
//根据账号提取密码
$sql = "select password from info where account={$account}";
$res = mysqli_query($conn,$sql) or meetError();
//先把数据搞出来
$arr =array();
$arr = mysqli_fetch_row($res);
if(mysqli_num_rows($res)!=1){
echo "账号密码出错";
header('Refresh:3,url=land.html');
exit;
}else if($arr[0]==$password) {
//成功登陆给浏览器设置cookie 让其七天免登陆
setcookie('is_land','1',time()+7*24*3600);
header('location:judge.php');
}else{
echo "密码不正确";
header('Refresh:3,url=land.html');
exit;
}
mysqli_close($conn);
?>
<html>
<!--注册功能的前端代码-->
<head>
<meta charset="utf8"/>
</head>
<p>账号注册</p>
<br/>
<form action="register.php" method="POST">
账号: <input type="text" name ='account' value=""/>
<br/>
<br/>
密码: <input type="password" name="password" />
<br/>
<input type="submit" name="submit" value="注册"/>
</form>
</html>
<?php
/*
*实现注册功能的后端代码
* 把register.html 中的数据存到数据库之中
*/
$account = $_POST['account'] or die("服务器错误 账号提取失败");
$password =$_POST['password'] or die ('服务器出错 密码提取失败');
//把数据放到已经建好的数据库之中 register 表名 info
//封装一个函数 数据库操作遇到错误的时候调用
function meetError(){
global $conn;
$error = mysqli_error($conn);
header('Refresh:3,url=register.html');
exit;
}
$conn = mysqli_connect('localhost','root',980613) ;
echo mysqli_connect_error($conn);
$sql = 'set names utf8';
mysqli_query($conn,$sql) or meetError();
$sql='use register';
mysqli_query($conn,$sql) or meetError();
//先判断账号密码是否为空
if(empty($account)||empty($password)){
echo "账号与密码不能为空";
header('Refresh:3,url=register.html');
exit;
}
//判断账号是否重复
$sql = "select id from info where account='{$account}';";
$res = mysqli_query($conn,$sql) or meetError();
if(mysqli_num_rows($res)!=0){
echo '此账号已被注册';
header('Refresh:3,url=register.html');
exit;
}
//把账号与密码insert into 数据库
$sql = " insert into info (account,password)
values('{$account}','{$password}')
";
$res = mysqli_query($conn,$sql);
if($res){
echo "注册成功";
header('Refresh:3,url = land.html');
exit;
}else{
echo "注册失败 ".mysqli_error($conn);
header('Refresh:3 url = register.html');
}
mysqli_close($conn);
?>
<?php
//判断是否登陆成功 没有登陆过的会被退出来
$is_land = $_COOKIE['is_land'] or die('请登陆后访问!');
if($is_land=="1"){
//加载成功登陆后的界面
include_once "success.html";
}else{
echo "请登录";
header('Refresh:3,url=land.html');
exit;
}
?>