<?php
function postback(){
if(empty($_POST['username'])){
$GLOBALS['message']='不能为空';
return;
}
if(empty($_POST['password'])){
$GLOBALS['message']='请输入密码';
return;
}
if(empty($_POST['confirm'])){
$GLOBALS['message']='密码不能为空';
return;
}
if($_POST['confirm']!==$_POST['password']){
$GLOBALS['message']="两次输入密码不一致";
return;
}
if(!(isset($_POST['agree'])&&$_POST['agree']==='on')){
$GLOBALS['message']="请同意协议";
return;
}
$username=$_POST['username'];
$password=$_POST['password'];
file_put_contents('users.txt',$username.'|'.$password."\n",FILE_APPEND);
$GLOBALS['message']='注册成功';
}
if ($_SERVER['REQUEST_METHOD']==='POST') {
postback();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="1">
<tr>
<td><label for="username">用户名</label></td>
<td><input type="text" name="username" id="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : ""; ?>"></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td><label for="confirm">用户名</label></td>
<td><input type="password" name="confirm" id="confirm"></td>
</tr>
<?php if(isset($message)): ?>
<tr>
<td></td>
<td><?php echo $message; ?></td>
</tr>
<?php endif ?>
<tr>
<td><button>注册</button></td>
<td><label><input type="checkbox" name="agree" value="true">同意注册协议</label></td>
</tr>
</table>
</form>
</body>
</html>
转载于:https://my.oschina.net/u/3848851/blog/1830509