2013-07-05 73 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; 
}