If-Else problem

Hey, I’m coding a some kind of filter which is being used to give out an echo whenever this link has been checked by the filter.

Example:


if ($_POST['check'] == '') {
        echo"Please enter a URL.";
}

elseif ($_POST['check'] == 
       'http://funpic.de' ||
       'http://knuddels-bots.com') {
       echo"This link is harmful.";
}

else {
       echo"This link is not harmful.";
}

Though, even If i check http://google.com, it says “Link is harmful”. I think it is because maybe it only identifies the first link, not the links below funpic.de. Anyone any idea how to prevent that?

You have to do this:

elseif ($_POST['check'] == 'http://funpic.de' ||
       $_POST['check'] == 'http://knuddels-bots.com') {
       echo "This link is harmful.";
}

Oh god, what a noob error of mine, - thanks. :slight_smile:

Here’s a slightly better, but not foolproof example.


<?php
function is_harmful_host($url, $harmful_hosts = array()){
  $host = parse_url($url, PHP_URL_HOST);
  foreach($harmful_hosts as $harmful_host){
    if(false !== stripos($host, $harmful_host)){
      return true;
    }
  }
  return false;
}

$hosts = array(
  'google.com',
  'google.co.uk'
);

var_dump(
  is_harmful_host('http://www.sitepoint.com/', $hosts)
); /* bool(false) */

var_dump(
  is_harmful_host('http://google.com/uk/?q=foo', $hosts)
); /* bool(true) */

var_dump(
  is_harmful_host('https://www.google.co.uk/', $hosts)
); /* bool(true) */
?>

You’ll have to bear in mind IP addresses though…