在开发登录部分时,我得到了一个错误。
结果总是显示错误语句。这段代码有什么问题
谁能指导我通过"preg_match",我想知道preg_match的细节。
代码是:
if (isset($_POST['submit']))
{
if (preg_match('%^[A-Za-z0-9]'S{8,20}$%', stripslashes(trim($_POST['userid']))))
{
$u = $_POST['userid'];
echo $u.'<br />';
}
else
{
$u = FALSE;
echo '<p><font color="red" size="+1">Please enter a valid User ID!</font></p>';
}
if (preg_match ('%^[A-Za-z0-9]'S{8,20}$%', stripslashes(trim($_POST['password']))))
{
$p =$_POST['password'];
$p=sha1($p);
echo $p;
}
else
{
$p = FALSE;
echo '<p><font color="red" size="+1">Please enter a valid Password!</font></p>';
}
}
在您的用户名验证器中,您只允许一个字符。在右括号后面加上长度说明符,就像在密码验证器中所做的那样。
为什么要限制密码中允许的字符?用户应该能够使用任何他想要的特殊字符,你也不应该限制最大长度。
%^[A-Za-z0-9]'S{8,20}$%
的意思是:A-Z或A-Z或0-9中的
18 ~ 20个
我怀疑你是这个意思。不妨试试[]
您可能想要以下内容:
<?php
if( isset( $_POST['submit'] ) ){
# An array to capture and hold errors - makes testing for, and displaying them
# easier down the track.
$errors = array();
# First Sanitising of the Form Data
$P = array_map( 'trim' , $_POST );
# User ID Check
if( !isset( $P['userid'] ) || $P['userid']=='' ){
$errors['userid'] = 'Please enter your desired User ID of between 8 and 20 characters/digits (no spaces)';
}elseif( !preg_match( '/[a-z0-9]{8,20}/i' , $P['userid'] ) ){
$errors['userid'] = 'Your User ID must contain between 8 and 20 characters/digits (no spaces)';
}
# Password Check
if( !isset( $P['password'] ) || $P['password']=='' ){
$errors['password'] = 'Please enter a password of between 8 and 20 characters/digits (no spaces)';
}elseif( !preg_match( '/[a-z0-9]{8,20}/i' , $P['userid'] ) ){
$errors['password'] = 'Your Password must contain between 8 and 20 characters/digits (no spaces)';
}
}
....
?><form>
User ID:
<?php echo ( isset( $errors['userid'] ) ? '<p><font color="red" size="+1">'.$errors['userid'].'</font></p>' : '' ); ?>
<input type="text" name="userid" value="<?php echo ( isset( $P['userid'] ) ? $P['userid'] : '' ); ?>" /><br>
Password:
<?php echo ( isset( $errors['password'] ) ? '<p><font color="red" size="+1">'.$errors['password'].'</font></p>' : '' ); ?>
<input type="password" name="password" /><br>
上面将需要一个UserID和密码,每个都在8到20个字符之间(a到Z或0到9)。如果提交表单并发现表单未通过验证测试,则显示表单,并在无效字段附近显示错误消息。它还包括提交的用户名(因此用户可以根据需要编辑它),但不包括密码(因为这通常只是不做)。
在谷歌上搜索一下PHP正则表达式(有很多教程和网站可以帮助你学习更多)。
还要看看MD5和SHA-1哈希系统,当你在数据库中存储密码时,你应该总是使用它们——否则,像索尼一样,如果有人破坏了你的系统,他们可以看到每个人的(可能重复使用的)密码,并可能破坏其他网站上的其他帐户。(不酷。)