2011-09-06 39 views
0

在开发登录部分时,出现错误。Preg_match混淆

结果总是显示了假statement.what的错误与此代码

任何人都可以指导我“的preg_match”,Wolud想知道详细的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>'; 
} 

}

+1

你确定你输入的用户名至少有9个字符? – codaddict

+0

我不知道到底在的preg_match你detail.Could只是解释 –

+0

@Peace情人:你看的文档(http://php.net/manual/en/function.preg-match.php)?如果是这样,在文档中有什么让你感到困惑的? – 2011-09-06 05:57:40

回答

2

%^ [A-ZA-Z0-9] \ S {8,20} $%

方式:

AZ的1或az或0-9 8通过\ S的20

我怀疑这就是你的意思?

也许尝试在\ S的[]

+0

试过,没有成功 –

+1

目标是什么?你想要做什么的英文描述是什么? – Corbin

+0

我的目标是通过登录 –

3

在您的用户名验证您只允许单个字符。将长度说明符放在右括号后面,就像您在密码验证程序中所做的一样。

为什么你的密码限制允许的字符?用户应该能够使用他想要的任何特殊字符,而且你也不应该限制最大长度。

+0

我已经添加了长度说明符,仍然没有工作 这是为了安全登录 –

+0

+1问为什么有一个上限和没有特殊字符允许。 – 2011-09-06 06:26:48

2

你可能会想要像下面这样:

<?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> 

以上将需要一个用户名和密码,每组8和20之间的字符( A到Z或0到9)。 如果提交表单并发现验证测试失败,则表单将显示错误消息,并且该字段无效。它还包括提交的用户名(因此用户可以根据需要编辑它),但不包括密码(因为这通常只是未完成)。

做一点谷歌搜索PHP正则表达式的(有大量的教程和网站在那里,以帮助您了解更多)。

还可以看看MD5和SHA-1哈希系统,在将密码存储到数据库中时应该始终使用这种系统 - 否则,就像索尼一样,如果有人危害您的系统,他们可以看到每个人(可能是重用的)密码,妥协其他网站上的其他帐户。 (不酷)