2011-02-05 49 views
0

我要讓我的網站黑客防爆所以這就是爲什麼我做的:

Text: mysql_real_escape_string($myVar); 
Number: (int)$myVar; 

我應該使用類似於文件由$myVar = $_FILE['myFile'];給定數組的東西嗎?

+0

這完全取決於你打算使用這些文件做什麼。最好展示一個真實世界的例子。 (作爲一個方面說明,你不應該在所有輸入數據上全局使用mysql_real_escape_string();只有在向數據庫中插入數據的時候,也應該注意這個函數只保護數據庫注入,除此之外別無它法。 int)`但是總是沒問題。) – 2011-02-05 17:26:45

+1

無論如何使用PDO,它比`mysql_real_escape_string()`更安全。 – Shoe 2011-02-05 17:28:03

回答

0

取決於用例。例如,如果您將文件名保存到數據庫,則應將其作爲字符串轉義。另外,您應該防止上傳和執行PHP腳本。

1

消毒文件名非常重要。

還有一些您可能想要覆蓋的問題,例如Windows中允許的所有字符都不允許在* nix中,反之亦然。一個文件名也可能包含一個相對路徑,可能會覆蓋其他未上傳的文件。從here

function Upload($source, $destination, $chmod = null) 
{ 
    $result = array(); 
    $destination = self::Path($destination); 

    if ((is_dir($destination) === true) && (array_key_exists($source, $_FILES) === true)) 
    { 
     if (count($_FILES[$source], COUNT_RECURSIVE) == 5) 
     { 
      foreach ($_FILES[$source] as $key => $value) 
      { 
       $_FILES[$source][$key] = array($value); 
      } 
     } 

     foreach (array_map('basename', $_FILES[$source]['name']) as $key => $value) 
     { 
      $result[$value] = false; 

      if ($_FILES[$source]['error'][$key] == UPLOAD_ERR_OK) 
      { 
       $file = ph()->Text->Slug($value, '_', '.'); 

       if (file_exists($destination . $file) === true) 
       { 
        $file = substr_replace($file, '_' . md5_file($_FILES[$source]['tmp_name'][$key]), strrpos($value, '.'), 0); 
       } 

       if (move_uploaded_file($_FILES[$source]['tmp_name'][$key], $destination . $file) === true) 
       { 
        if (self::Chmod($destination . $file, $chmod) === true) 
        { 
         $result[$value] = $destination . $file; 
        } 
       } 
      } 
     } 
    } 

    return $result; 
} 

採取

此上傳功能的重要組成部分是:

1)確保該文件不包含任何相對路徑。

2)ph()->Text->Slug(),這使得確保只有.0-9a-ZA-Z被允許在文件名中,所有其它字符是由下劃線(_)

3)md5_file()取代,這是添加到當且僅當文件名具有相同名稱的另一個文件已經存在

看看如何以及其explained here

相關問題