2011-05-02 48 views
12

我是編程和PHP領域的新手,因此想要了解清理表單數據以避免格式不正確的頁面,代碼注入等的最佳方式。我在下面的示例中找到了示例腳本嗎?PHP淨化數據

代碼最初發佈於http://codeassembly.com/How-to-sanitize-your-php-input/

/** 
* Sanitize only one variable . 
* Returns the variable sanitized according to the desired type or true/false 
* for certain data types if the variable does not correspond to the given data type. 
* 
* NOTE: True/False is returned only for telephone, pin, id_card data types 
* 
* @param mixed The variable itself 
* @param string A string containing the desired variable type 
* @return The sanitized variable or true/false 
*/ 

function sanitizeOne($var, $type) 
{  
    switch ($type) { 
    case 'int': // integer 
     $var = (int) $var; 
     break; 

    case 'str': // trim string 
     $var = trim ($var); 
     break; 

    case 'nohtml': // trim string, no HTML allowed 
     $var = htmlentities (trim ($var), ENT_QUOTES); 
     break; 

    case 'plain': // trim string, no HTML allowed, plain text 
     $var = htmlentities (trim ($var) , ENT_NOQUOTES) ; 
     break; 

    case 'upper_word': // trim string, upper case words 
     $var = ucwords (strtolower (trim ($var))); 
     break; 

    case 'ucfirst': // trim string, upper case first word 
     $var = ucfirst (strtolower (trim ($var))); 
     break; 

    case 'lower': // trim string, lower case words 
     $var = strtolower (trim ($var)); 
     break; 

    case 'urle': // trim string, url encoded 
     $var = urlencode (trim ($var)); 
     break; 

    case 'trim_urle': // trim string, url decoded 
     $var = urldecode (trim ($var)); 
     break; 

    case 'telephone': // True/False for a telephone number 
     $size = strlen ($var) ; 
     for ($x=0;$x<$size;$x++) 
     { 
      if (! ((ctype_digit($var[$x]) || ($var[$x]=='+') || ($var[$x]=='*') || ($var[$x]=='p')))) 
      { 
       return false; 
      } 
     } 
     return true; 
     break; 

    case 'pin': // True/False for a PIN 
     if ((strlen($var) != 13) || (ctype_digit($var)!=true)) 
     { 
      return false; 
     } 
     return true; 
     break; 

    case 'id_card': // True/False for an ID CARD 
     if ((ctype_alpha(substr($var , 0 , 2)) != true) || (ctype_digit(substr($var , 2 , 6)) != true) || (strlen($var) != 8)) 
     { 
      return false; 
     } 
     return true; 
     break; 

    case 'sql': // True/False if the given string is SQL injection safe 
     // insert code here, I usually use ADODB -> qstr() but depending on your needs you can use mysql_real_escape(); 
     return mysql_real_escape_string($var); 
     break; 
    }  
    return $var; 
} 
+0

它看起來非常有用。雖然'htmlentities'應該被替換爲'htmlspecialchars'並聲明字符集參數。 – mario 2011-05-02 23:26:40

回答

9

你的示例腳本不是很大 - 所謂的字符串sanitisation只是修剪每一端的空白。依靠它會讓你很快陷入困境。

沒有一個適合所有解決方案。您需要爲您的應用程序實施正確的安全措施,這完全取決於您需要什麼輸入以及它在哪裏使用。在任何情況下,你都應該在多個層次進行消毒 - 很可能當你接收到數據,當你存儲數據時,或許你在渲染數據時。

值得一讀,可能受騙者:

What's the best method for sanitizing user input with PHP?

Clean & Safe string in PHP

+0

謝謝。你如何測試所有可能的場景或至少一個子集,如果這些? – PeanutsMonkey 2011-05-03 00:43:55

+0

@PananutsMonkey - 在你的例子中使用的_approach_很好 - 即調用一個通用的sanitisation函數,它只允許在你明確定義的數據中使用你特別要求的類型。這部分是好的。 問題是衛生需要足夠強大。清理數字很簡單(類似'$ i =(float)$ i;'),問題通常是使用字符串,因爲這些數據容易受到SQL注入和XSS攻擊。 你真的需要知道你的字符串被用來做什麼。例如。他們必須支持外國人物嗎? – 2011-05-03 10:42:19

4

它不壞。

對於SQL,通過使用PDO向參數中插入參數,最好避免風險。

+0

你可以用mysqli來實現。如果你只使用MySQL,這可能會稍微好一點。 [mysqli prepared statements](http:// php。net/manual/en/mysqli.quickstart.prepared-statements.php) – Lightbulb1 2015-06-05 12:06:03

18

這個腳本有一些很好的功能,但是它在消毒方面做得不好!

根據你所需要的(並且要接受),可以使用:

+1

謝謝。使用正則表達式去除特殊字符更好嗎?爲什麼不使用htmlspecialchars? – PeanutsMonkey 2011-05-02 23:46:37