<?php
// 设置一个函数,使得可以使用return
function postback(){
// 申明$massage是全局的
global $massage;
// 判断是否输入了账号,如果没输入给出提示,并结束信息提交
if(empty($_POST['username'])){
$massage= '请输入账号';
return;
}
// 判断密码
if(empty($_POST['password'])){
$massage= '请输入密码';
return;
}
// 判断两次密码是否一致
if($_POST['password']!==$_POST['Confirm']){
$massage= '两次密码不一致';
return;
}
// 判断是否勾选了注册协议
if(!(isset($_POST['Agreement']) && $_POST['Agreement']==='on') ){
$massage= '请勾选用户协议';
return;
}
//获得用户所输入的账号密码
$username= $_POST['username'];
$password= $_POST['password'];
// 将账号密码存入硬盘(持久化),存到users.txt
// 用丨分隔账号密码,"\n"进行每一次的换行,FILE_APPEND对账号密码进行追加
file_put_contents('users.txt',$username.'丨'.$password."\n", FILE_APPEND);
$massage= '注册成功';
}
//判断是否是post提交
if($_SERVER['REQUEST_METHOD']==='POST'){
//调用函数
postback();
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>注册</title>
</head>
<body>
<!-- action永远提交到本页面 -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<!-- label中的for与id关联,使得点击文字的时候也可以选到input -->
<label for="username">账号:</label>
<!-- 表单保持,当用户输入了错误的信息重新输入的时候保存用户上一次输入的账号,并显示 -->
<input name="username" id="username" value="<?php echo isset($_POST['username']
)? $_POST['username']:''?>" />
<br />
<label for="password">密码:</label>
<input type="password" name="password" id="password" />
<br />
<label for="Confirm">确认密码:</label>
<input type="password" name="Confirm" id="Confirm" />
<br />
<!-- 设置一个value给后台判断 -->
<label><input type="checkbox" name="Agreement" value="on"/>同意协议</label>
<br />
<!-- 将注册状态信息显示到页面上,当没有错误信息的时候不显示 -->
<?php if(isset($massage)):?>
<p><?php echo $massage; ?></p>
<?php endif ?>
<!-- button自带submit属性 -->
<button>注册</button>
</form>
</body>
</html>