2013-07-05 78 views
0

我正在開發一個小項目,並且因腦死亡,所以我希望這裏有人能夠幫助我打敗我的編碼器塊。退出foreach循環並檢索結果

我試圖創建一個頁面使用php,根據什麼(如果有的話)值傳遞給頁面(位置)來更改其內容顯示。我創建了一個安全列表陣列,我已經存儲了不同的位置。首先我檢查傳遞給安全列表的任何值,如果它匹配我顯示一組內容。

如果它不匹配我正在運行相似性測試來檢查它們是否可能是簡單的錯字,並且仍然可以將用戶導航到頁面我認爲他們想要但這是我陷入困境的地方。

我希望有人可以鍵入

www.example.co.uk/location.php < ----加載通用位置頁

www.example.co.uk/ location.php?loc = Bishops-Stortford < ----加載目標位置頁面

www.example.co.uk/location.php?loc=Bishop-Stortford < ----加載目標位置儘管錯誤提供了90%或更多的匹配,但位置頁面仍然存在

www.example.co.uk/location.php?loc=?php回聲「我黑了你的網站」; ?> ----希望我的系統能夠解除令人討厭的代碼注入問題

我會在下面發佈我的代碼,以便您能夠看到我所得到的結果。

<?php 
     $loc = ""; 
     $safelist = array("Bishops Stortford", "Braintree", "Chelmsford", "Dunmow", "Harlow", "Hertford", "Saffron Walden", "Sawbridgeworth", "Stansted", "Ware", 
        "Essex", "Hertfordshire"); 

     if(isset($_GET["loc"])) { 
      /* Gets the value of loc if set, replaces hyphens with spaces and capitalises first letters of words converting the rest to lowercase. */ 
      $loc = ucwords(strtolower(str_replace("-", " ", $_GET["loc"]))); 
     } 

     /* Is word in safelist */ 
     if (in_array($loc, $safelist)) { 
      /* Yes */ 
      if (($loc == "Essex") or ($loc == "Hertfordshire")) { 
       $county = True; 
      } else { 
       $county = False; 
      } 

      if ($county == False) { 
       echo "\"" . $loc . "\" is not a county"; 
      }else{ 
       echo "\"" . $loc . "\" is a county"; 
      } 
     } else { 
      /* No, Is string 90% similar to any entry within the safelist? */ 
      foreach ($safelist as $safeword) {  
       similar_text($safeword, $loc, $percent); 
       echo $safeword . " " . $loc . " " . $percent . "<br />"; 

       if ($percent >= 90) { 

      } 
     } 


    ?> 

我不知道該怎麼辦if($ percent> = 90)。我知道我想退出循環,並從我發現的第一次90%或更多的比賽中得到結果,但我不是100%確定如何做到這一點。

什麼是最好的方式來處理代碼注入,如www.example.co.uk/location.php?loc=?php回聲「我砍了你的網站」; ?>

+0

既然你不調用'eval($ loc)',將PHP語句放入它將不起作用。 – Barmar

+0

我不明白這個問題。您所回覆的所有內容都將顯示在頁面上,您無需執行任何操作即可檢索結果。 – Barmar

+0

我將把回聲帶出,在那裏進行調試。我在頭腦中看到的方式是使用foreach循環將數組中的每個字符串與傳入頁面的字符串進行比較。 當百分比值達到90%或出現時,我退出循環並從緊密匹配的數組中取值,然後使用該值加載我的頁面內容。 如果在90%範圍內找不到匹配項,那麼我想處理代碼就好像它的惡意 –

回答

1

我想這是你想要什麼:

 foreach ($safelist as $safeword) {  
      similar_text($safeword, $loc, $percent); 
      echo $safeword . " " . $loc . " " . $percent . "<br />"; 
      if ($percent >= 90) { 
       $loc = $safeword; 
       $county = true; 
       break; 
      } 
     } 

只要你不用戶輸入呼叫eval(),你不必擔心它們注入PHP語句。當你迴應一些東西時,它會被髮送到瀏覽器,它不會被PHP執行。但是,您仍然應該對輸出進行清理,因爲它可能包含HTML標記,甚至可能包含JavaScript,這可能會劫持用戶的瀏覽器。當頁面上顯示輸出,使用htmlentities()對它進行編碼:

echo "Greetings, " . htmlentities($first_name); 
+0

謝謝Barmar。我用你的代碼,並適應它進行調試,所以它讀取 - 的foreach($安全列表爲$的SafeWord){\t \t \t \t \t \t \t similar_text($的SafeWord,$祿,$%)的; \t \t \t \t \t echo $ safeword。 「」。 $ loc。 「」。 $百分比。 「
」; \t \t \t \t \t \t \t \t \t \t如果($百分比> = 90){ \t \t \t \t \t \t $ LOC = $的SafeWord; \t \t \t \t \t \t回聲 「爆發循環的,因爲」。 $ loc。 「匹配在」。 $百分比。 「%」; \t \t \t \t \t \t break; \t \t \t \t \t \t \t \t \t \t \t} –

+0

我加了一點建議有關消毒輸出到HTML頁面。 – Barmar

0

我想我會重新調整它,像這樣:

$loc = ""; 
$safelist = array("Bishops Stortford", "Braintree", "Chelmsford", "Dunmow", "Harlow", "Hertford", "Saffron Walden", "Sawbridgeworth", "Stansted", "Ware", 
       "Essex", "Hertfordshire"); 
if(isset($_GET["loc"])) { 
    /* Gets the value of loc if set, replaces hyphens with spaces and capitalises first letters of words converting the rest to lowercase. */ 
    $loc = ucwords(strtolower(str_replace("-", " ", $_GET["loc"]))); 
} 
$good = ''; 
if (in_array($loc, $safelist)) { 
    $good = $loc; 
} else { 
    foreach ($safelist as $safeword) {  
     similar_text($safeword, $loc, $percent); 
     echo $safeword . " " . $loc . " " . $percent . "<br />"; 
     if ($percent >= 90) { 
      $good = $safeword; 
     } 
    } 
} 
if (! empty($good)){ 
    /* Yes */ 
    if (($good == "Essex") or ($good == "Hertfordshire")) { 
     $county = True; 
    } else { 
     $county = False; 
    } 

    if ($county == False) { 
     echo "\"" . $good . "\" is not a county"; 
    }else{ 
     echo "\"" . $good . "\" is a county"; 
    } 
    //And whatever else you want to do with the good location... 
} 

像Barmar說,因爲你沒有做任何與除了將其與數組進行比較之外,輸入值不存在以這種方式發生攻擊的風險。

0

要回答你的問題的第二部分,我用htmlentities輸出數據直接從輸入和類似的數據此功能的屏幕之前保存到數據庫:

function escape_value($value) 
{ 
    if($this->real_escape_string_exists) 
    { 
     if($this->magic_quotes_active){$value = stripslashes($value);} 
     $value = mysql_real_escape_string($value); 
    } 
    else 
    { 
     if(!$this->magic_quotes_active){$value = addslashes($value);} 
    } 
    return $value; 
}