2012-02-13 62 views
1

首先,我不想使用任何框架,但我正在寻找使用白名单验证的好方法。我将在所有用户输入中应用它,我需要验证XSS保护,并且还希望应用不同的格式,例如:php验证概念

示例1 XSS。

<input type="text" name="test" value="<script>alert('test');</script" /> 

实施例2日期。

<input type="text" name="test" value="31-05-2012" /> 

例3时间。

<input type="text" name="test" value="15:00" /> 

例4最大长度。

<input type="text" name="test" value="short description" /> 

实施例5最小长度。

<input type="text" name="test" value="min description" /> 

例6个字母和默认符号只

<input type="text" name="test" value="hello world. This is à ö text input :P :) :S :$ =D !! ??" /> 

例7数字只有

<input type="text" name="test" value="1234567890" /> 

我的想法是建立一个客户方和服务器的网站验证,如果用户被穿过客户端验证(jQuery),他们将被标记为黑客,因为默认用户不可能通过客户端验证。

我的问题是:将客户端+服务器端验证应用于防止XSS并在字段上应用正则表达式的最佳方式是什么?有没有用于验证的轻量级PHP库?

我已经看过:

ctype_alpha 
preg_match 

但我没有放弃知道什么是使用最好的之一,而ctype_alpha不会允许缺省符号等

任何建议?例子?感谢您的时间和阅读,并为这个忙碌的问题感到抱歉。

+0

那些没有JavaScript支持的用户呢? – Gumbo 2012-02-13 15:14:01

+0

@gumbo他们不能去web应用程序,因为它严重依赖于Javascript。 – randomKek 2012-02-13 15:16:58

+1

看来你正在使用术语“白名单”错误 – 2012-02-13 15:17:10

回答

1

看来你只需要一些基本的验证,而不是“白名单”。

这个想法很简单。

  1. 创建服务器端验证。与ctype_alpha,preg_match等。 (我希望你的问题不是从头开始教你这些功能)。
  2. 如果需要,可以通过将AJAX调用到您用于(1)的相同验证例程来创建cleint端验证。
  3. 当然,你必须同时使用这两个。
  4. 将用户标记为黑客似乎不是最好的主意。你会对标记用户做什么?
+0

Sharpnel什么是白名单验证,如果我错了呢?我的整个应用程序都依赖于ajax,没有javascript的用户将会得到我覆盖的黑屏。接下来的问题是,如果每一篇文章,我的网站上的每个数据都是基于ajax的,那么为什么还要使用客户端验证来验证它是否会通过服务器端验证呢?我知道这是一些资源,但真正的差异是值得注意的吗? – randomKek 2012-02-13 15:24:01

+0

客户端验证意味着内联验证。您可以在用户输入用户名后立即验证用户名,而不是等到提交完整表单后 – 2012-02-13 15:26:22

+0

如果我们正在调用服务器端验证,为什么还要使用客户端验证? – randomKek 2012-02-13 15:27:20

1

我遇到了类似的问题,最后写了我自己的“Input-Datatype”类。如果你只用它来验证输入,这可能有点过分。但是你可以建立使用PHP函数如的preg_match,is_numeric,等的strtotime混合验证功能...

日期验证的一个例子是:

public function validate(&$value) { 

    $date = strtotime($value); 

    if($date === false){ 
     //Error no valid date 
    }else{ 

    if(isset($this->maxDate)){ 
     if($date>strtotime($this->maxDate)){ //maxDate being the maximal date allowed 
      //Error max date exceeded 
     } 
    } 

    if(isset($this->minDate)){ 
     if($date<strtotime($this->minDate)){ //minDate being the minimal date allowed 
      //Error date too low 
     } 
    } 

    $value = strftime($this->format,$date); //format being the format in which the date should be saved 
} 

用于验证文本可能是另一个例子:

public function validate(&$value) { 

    if (isset($value) && $value != "") { 
     if(isset($this->maxLength)&&$this->maxLength!= ""){ //maxLength being the maximal number of characters 
      if (strlen($value) > $this->maxLength) { 
       //Error max length exceeded 
      } 
     } 
    } else { 
     if (!$this->allowNull) { //allowNull being a boolean: true if text can be empty 
      //Error value is empty 
     } 
    } 

    if(isset($this->regex)&&$this->regex!= ""){ //regex can be any regular expression, e.g: /[A-Za-z]/ for letters only 
     if(!preg_match($this->regex, $value)){ 
      //Error value does not match expression 
     } 
    } 
} 

至于XSS去,请确保您与数据库交互时使用prepared statements和显示用户输入数据时使用htmlentities

希望这会有所帮助。

0

前段时间,我写了一个轻量级验证类。也许你可以使用它。

例如:

$oValidator = new Validator(); 
$oValidator->setLanguage('en'); 
$oValidator->isValid('short description', 'max_length[4]'); 
echo $oValidator->getLastErrorMessage(); 
//The input can not exceed 4 characters in length. 

$oValidator->isValid('min description', 'min_length[5]'); 
$oValidator->isValid('hello world. This is à ö text input :P :) :S :$ =D !! ??', 'min_length[5]'); 
$oValidator->isValid('1234567890', 'digits'); 

规则定义:

/** 
* @ErrorMessage[lang=de] Die Eingabe muss mindestens %d Zeichen lang sein. 
* @ErrorMessage[lang=en] The input must be at least %d characters in length. 
*/ 
public function check_min_length($mValue, $aParams) 
{ 
    return (strlen($mValue) >= $aParams[0]); 
} 

实施例: http://sklueh.de/2013/01/lightweight-php-validator-neue-version/

github上: https://github.com/sklueh/Lightweight-PHP-Validator