php登录表单验证,代码:
<?php
function validate_user ($username, $password){
return true;
}
// 创建一个空数组存储错误消息
$errors = array();
$p =& $_POST;
if (count ($p) > 0){
if (!isset ($p['username']) || (trim ($p['username'])
== '')){
$errors[] = 'You must enter a username.';
}elseif{ ((strlen ($p['username']) < 8) || (ereg
('[^a-zA-Z0-9]', $p['username']))){
$errors[] = '
你没有输入一个有效的用户名。
用户名必须
至少8个字符长,可以
只包含
字母和数字。
';
}
if (!isset ($p['password']) || (trim ($p['password'])
== '')){
$errors[] = 'You must enter a password.';
}elseif ((strlen ($p['password']) < 8) || (ereg
('[^[:alnum:][:punct:][:space:]]', $p['password']))){
$errors[] = '
你没有输入一个有效的密码。
密码必须
至少8个字符长,可以
只包含
字母,数字,标点符号和
空格
.';
}
if (count ($errors) == 0) {
$r = validate_user ($p['username'],
$p['password']);
if ($r == false){
$errors[] = '登录失败,用户名或密码不存在。';
} else {
print
('<html><head><title>Congratulations</title><
/head>
<body><h1>祝贺你!</h1><p>You
成功登录!</p>
</body></html>');
exit;
}
}
}
?>
<html>
<head><title>登录表单-www.jb200.com</title></head>
<body>
<h1>Login Form</h1>
<?php
if (count ($errors) > 0) {
$n = count ($errors);
for ($i = 0; $i < $n; $i++){
print '<br /><font color="red">' .
$errors[$i] . '</font>';
}
}
?>
<form action="<?php print ($PHP_SELF); ?>" method="POST">
<table>
<tr><td>用户名:</td>
<td><input type="text" name="username" value="<?php if
(isset ($p['username'])) print $p['username']; ?>"
/></td>
</tr>
<tr><td>密码:</td>
<td><input type="text" name="password" value="<?php if
(isset ($p['password'])) print $p['password']; ?>"
/></td>
</tr>
<tr><td colspan="2"><input type="submit"
name="submit"></td></tr>
</table>
<input type="hidden" name="__process_form__" value="1"
/>
</form>
</body>
</html>