循环问题和重复结果

时间:2017-12-14 13:47:54

标签: php loops

我已经创建了一个脚本来检查响应头并检查它是否匹配和我已经在其上定义了一些值的数组, 你可以在这里找到代码:

$url = 'https://www.google.com.bh/';
    $result= get_headers($url,1);
$search = array('Strict-Transport-Security','Content-Security-Policy','X-Frame-Options','X-XSS-Protection',
        'X-Content-Type-Options','Referrer-Policy');
    //print_r($search);

     echo '<table class="table"><tbody><tr><td><b>HTTP Security Header</b></td><td><b>Header Role</b></td><td><b>Status</b></td></tr>';
    $i=0;
    foreach ($result as $key  => $v) {

    list($a,$b,$c,$d,$e,$f) = $search;

        if(stristr($key,$a)) {
        echo '<tr><td>'.$a.'</td><td>Protects against man-in-the-middle attacks</td><td> set</td></tr>';
        }else
        echo '<tr><td>'.$a.'</td><td>Protects against man-in-the-middle attacks</td><td>Not set</td></tr>';
        if(stristr($key,$b)) {
        echo '<tr><td>'.$b.'</td><td>Prevents possible phishing or XSS attacks</td><td> set</td></tr>';
        }else 
        echo '<tr><td>'.$b.'</td><td>Prevents possible phishing or XSS attacks</td><td>Not set</td></tr>';
        if(stristr($key,$c)) {
         echo '<tr><td>'.$c.'</td><td>Protects against Clickjacking attacks</td><td> set</td></tr>';
        }else 
         echo '<tr><td>'.$c.'</td><td>Protects against Clickjacking attacks</td><td>Not set</td></tr>';

        if(strstr($key,$d)) {
        echo '<tr><td>'.$d.'</td><td>Mitigates Cross-Site Scripting (XSS) attacks</td><td> set</td></tr>';
        }else 
        echo '<tr><td>'.$d.'</td><td>Mitigates Cross-Site Scripting (XSS) attacks</td><td>Not set</td></tr>';

        if(stristr($key,$e)) {
        echo "Found".$e;
        }else 
        echo '<tr><td>'.$e.'</td><td>Prevents possible phishing or XSS attacks</td><td>Not set</td></tr>';

        if(stristr($key,$f)) {
        echo '<tr><td>'.$f.'</td><td>Prevents possible phishing or XSS attacks</td><td>Not set</td></tr>';
        }else 
        echo '<tr><td>'.$f.'</td><td>Protects against Clickjacking attacks</td><td>Not set</td></tr>';
        }

我希望给出一个结果,但是当我运行脚本时,它会提供重复的输出。 请说明代码有什么问题 谢谢,enter image description here

1 个答案:

答案 0 :(得分:0)

问题在于您对每个标题行进行测试,因此您检查每个标题行 - 然后针对所有可能性执行if...else...条件集。由于每个标题只能是一种类型,这意味着它将失败所有其他标题并输出它们是“未设置”#。

如果您只是检查是否在数组中设置了该标头,它只会检查一次...

$url = 'https://www.google.com.bh/';
$result= get_headers($url,1);
foreach ( $result as $key=>$header )  {
    $result[strtoupper($key)] = $header;
}    
$search = array('Strict-Transport-Security','Content-Security-Policy','X-Frame-Options','X-XSS-Protection',
        'X-Content-Type-Options','Referrer-Policy');
//print_r($search);

echo '<table class="table"><tbody><tr><td><b>HTTP Security Header</b></td><td><b>Header Role</b></td><td><b>Status</b></td></tr>';

list($a,$b,$c,$d,$e,$f) = $search;

if ( isset( $result[strtoupper($a)] )) {
    echo '<tr><td>' . $a . '</td><td>Protects against man-in-the-middle attacks</td><td> set</td></tr>';
} else  {
    echo '<tr><td>' . $a . '</td><td>Protects against man-in-the-middle attacks</td><td>Not set</td></tr>';
}

因此,使用if (isset ($result[$a] )) {检查该标头是否位于结果标头列表中。您需要为其他每个代码添加相同的代码,但希望这能告诉您如何完成它。