【问题标题】:Exiting a foreach loop and retrieve result退出 foreach 循环并检索结果
【发布时间】:2013-07-05 17:30:26
【问题描述】:

我正在做一个小项目,但我已经脑死了,所以我希望这里有人可以帮助我克服我的编码障碍。

我正在尝试使用 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

www.example.co.uk/location.php?loc=?php echo "I hacked your site"; ?> ---- 希望我的系统能够解除讨厌的代码注入

我将在下面发布我的代码,以便您查看我的内容。

<?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) {

            }
        }


    ?>

我想不出如果 ($percent >=90) 该怎么办。我知道我想退出循环并从我找到的前 90% 或更多匹配中获取结果,但我不是 100% 确定如何执行此操作。

还有什么是处理代码注入的最佳方法,例如 www.example.co.uk/location.php?loc=?php echo "I hacked your site"; ?>

【问题讨论】:

  • 因为你没有调用eval($loc),所以在里面放PHP语句是没有效果的。
  • 我不明白这个问题。您回显的所有内容都将显示在页面上,您无需执行任何操作即可检索结果。
  • 我会把回声拿出来,它在那里进行调试。我在脑海中看到的方式是使用 foreach 循环将数组中的每个字符串与传递到页面中的字符串进行比较。当百分比值为 90% 或出现时,我退出循环并从数组中获取匹配的值,然后使用该值加载我的页面内容。如果在 90% 范围内没有找到匹配项,那么我想将代码视为恶意代码

标签: php foreach


【解决方案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);

【讨论】:

  • 谢谢巴马尔。我使用了您的代码并对其进行了调试,因此它显示为 - foreach ($safelist as $safeword) {similar_text($safeword, $loc, $percent);回声 $safeword 。 “”。 $loc 。 “”。 $% 。 "
    "; if ($percent >= 90){ $loc = $safeword;回声“打破循环,因为”。 $loc 。 " 匹配 " 。 $% 。 "%";休息; }
  • 我添加了一些关于清理输出到 HTML 页面的建议。
【解决方案2】:

为了回答您问题的第二部分,我使用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;
}

【讨论】:

    【解决方案3】:

    我想我会重组它,像这样:

    $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 所说,由于除了将输入值与数组进行比较之外,您没有对输入值做任何事情,因此不存在以这种方式受到攻击的风险。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-05
      • 2017-12-22
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多