dongtuoao7987 2015-02-28 20:33
浏览 23
已采纳

PHP有害的URL保护

I've made this script, but the 4th line isn't right and I have really no clue how to solve this. I really appriciate if someone helps me. This is my code:

<?php
$url = $_GET["url"];
$badsite = array("http://check.com", "http://hotmail.com");

if($url == $badsite) {
echo "This URL is harmful.";
} else {
echo "Not harmful";
header("Location: " . $_GET["url"]);
}
?>

So the thing which doesn't work is the following line

if($url == $badsite) {

How can I make it so it checks if the GET contains a $badsite?

  • 写回答

2条回答 默认 最新

  • dongzong7467 2015-02-28 20:35
    关注

    You don't want to check if the value equals the array, you want to check if it's in the array. Perhaps something like this:

    if (in_array($url, $badsite)) {
      // ...
    }
    

    Side note, you don't need (or want, really) this echo statement:

    echo "Not harmful";
    header("Location: " . $_GET["url"]);
    

    You might get an error by emitting output before sending a header. But even if you buffer output or in some other way suppress that error, there's no reason to emit output when returning a redirect response. The browser would display it for only an instant, if at all. A redirect by itself is a complete HTTP response, no output is required.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?