模拟用户登陆验证
<?php
$code = '<span style="color:rgb(' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ')">' . mt_rand( 0, 9 ) . '</span>';
$code .= '<span style="color:rgb(' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ')">' . mt_rand( 0, 9 ) . '</span>';
$code .= '<span style="color:rgb(' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ')">' . mt_rand( 0, 9 ) . '</span>';
$code .= '<span style="color:rgb(' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ' ' . mt_rand( 0, 255 ) . ')">' . mt_rand( 0, 9 ) . '</span>';
function regpost()
{
global $massage;
global $code;
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$ayuan = $_POST['ayuan'];
$massage = '注册成功';
if ( empty( $username ) ) {
$massage = '请输入账号';
return;
}
if ( ord( $username ) < 65 || ord( $username ) > 90 && strlen( $username ) < 6 ) {
$massage = '用户名首个字符必须是字母且大于6位';
return;
}
if ( empty( $password ) ) {
$massage = '请输入密码';
return;
}
if ( $password2 !== $password ) {
$massage = '两次密码不一致';
return;
}
if ( empty( $ayuan ) ) {
$massage = '请输入验证码';
return;
}
if ( strcasecmp( $ayuan, $code ) !== 0 ) {
$massage = '验证码不正确';
return;
}
}
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
regpost();
}
?>
<!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>
<form action = "" method = 'post'>
<label>账号:</label>
<input name = 'username' value = "<?php echo isset($_POST['username']) ? $_POST['username'] : '' ?>" />
<br />
<label>密码:</label>
<input type = 'password' name = 'password' />
<br />
<label>确认密码:</label>
<input type = 'password' name = 'password2' />
<br />
<label>验证码:
<input type = 'text' name = 'ayuan' style = 'width: 50px;'>
<?php
echo $code;
?></label>
<br />
<?php if ( isset( $massage ) ) : ?>
<p><?php echo $massage;
?></p>
<?php endif ?>
<button>注册</button>
</form>
</body>
</html>