adv Posted April 21, 2008 Share Posted April 21, 2008 hello i`ve tried to create a script that when hosts that are in the array() will show a error page but i`m stuck after that i want to show the page if the host is not in the array() here`s the code <?php $uri = $_SERVER["REQUEST_URI"]; $ipserver = $_SERVER["SERVER_ADDR"]; $resuelveserver = gethostbyaddr($ipserver); $IP = getenv("REMOTE_ADDR"); $host = gethostbyaddr($IP); $banhosts = array(".us","email",".cc",".de",".hk"); $x = count($banhosts); $notfound = "<HTML><HEAD> <TITLE>404 Not Found</TITLE> </HEAD><BODY> <H1>Not Found</H1> The requested URL $uri was not found on this server.<P> <HR> <ADDRESS>Apache/1.3.34 Server at $resuelveserver Port 80</ADDRESS> </BODY></HTML>"; for ($y = 0; $y < $x; $y++) { if (strpos($host ,$banhosts[$y])=== true) { echo $notfound; } } ?> after the if i`ve tried alot of things but nothing elseif (strpos($host ,$banhosts[$y])=== false) { echo "you are allowed here";} but it doesn`t echo nothing elseif (!strpos($host ,$banhosts[$y])) { echo "you are allowed here";} same here i`ve tried directly else { echo "you are allowed here"; } but it keeps echoing even if the host is in the banhosts array() Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/ Share on other sites More sharing options...
dptr1988 Posted April 21, 2008 Share Posted April 21, 2008 strpos does not return 'true' it only returns the offest of the matching string or 'false' for error. Try this: <?php for ($y = 0; $y < $x; $y++) { if (strpos($host ,$banhosts[$y]) !== false) { echo $notfound; } } ?> Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-522328 Share on other sites More sharing options...
adv Posted April 21, 2008 Author Share Posted April 21, 2008 pff it still doesn`t work : $banhosts = array(".us","email",".cc",".de",".hk"); for ($y = 0; $y < $x; $y++) { if (strpos($host ,$banhosts[$y]) !== false) { echo $notfound; } else { echo "ad"; } } and i tried with my ip that is in the $banhosts but if i use the else { echo "ad"; } adad Not Found The requested URL /test22.php was not found on this server. Apache/1.3.34 Server at localhost Port 80 i don`t know what is wrong if i try with only the if statement and my host in in the $banhosts it works but if my host is not in the $banned hosts it doesnt show nothing Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-522713 Share on other sites More sharing options...
dptr1988 Posted April 21, 2008 Share Posted April 21, 2008 Have you checked that '$host' is an actual hostname? gethostbyaddr($IP); may not alwasy work. If you are having trouble with an 'if' statement, it's always a good idea to print out all of the variables used in that statement with var_dump(); Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-522795 Share on other sites More sharing options...
adv Posted April 21, 2008 Author Share Posted April 21, 2008 yes $host is an actual hostname $banhosts = array("email","usd","rds"); i tried with var_dump() as u said i saw this : var_dump($host,$banhosts[$y]); string(23) "239-12-92-86.rdsnet.ro." NULL the $banhosts[$y] is returning NULL why ?!?! Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-522835 Share on other sites More sharing options...
adv Posted April 22, 2008 Author Share Posted April 22, 2008 anyone!?!? Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-523480 Share on other sites More sharing options...
dptr1988 Posted April 22, 2008 Share Posted April 22, 2008 This seems to work in my server. Try it out on your server <?php //$uri = $_SERVER["REQUEST_URI"]; //$ipserver = $_SERVER["SERVER_ADDR"]; //$resuelveserver = gethostbyaddr($ipserver); //$IP = getenv("REMOTE_ADDR"); //$host = gethostbyaddr($IP); // For debugging $host = "239-12-92-86.rdsnet.ro."; $banhosts = array(".us","email",".cc",".de",".hk", ".ro"); $x = count($banhosts); //$notfound = "<HTML><HEAD> //<TITLE>404 Not Found</TITLE> //</HEAD><BODY> //<H1>Not Found</H1> //The requested URL $uri was not found on this server.<P> //<HR> //<ADDRESS>Apache/1.3.34 Server at $resuelveserver Port 80</ADDRESS> //</BODY></HTML>"; var_dump($banhosts); for ($y = 0; $y < $x; $y++) { $result = strpos($host ,$banhosts[$y]); echo "Result for {$banhosts[$y]}: " . var_dump($result) . "\n"; if ($result !== false) { // echo $notfound; echo "Banned Host"; } } ?> Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-523490 Share on other sites More sharing options...
adv Posted April 22, 2008 Author Share Posted April 22, 2008 thanks alot dptr for helping me but i got still a problem <?php $uri = $_SERVER["REQUEST_URI"]; //$ipserver = $_SERVER["SERVER_ADDR"]; $resuelveserver = gethostbyaddr($ipserver); //$IP = getenv("REMOTE_ADDR"); //$host = gethostbyaddr($IP); // For debugging $host = "239-12-92-86.rdsnet.ro."; $banhosts = array(".us","email",".cc",".de",".hk",".ro"); $x = count($banhosts); $notfound = "<HTML><HEAD> <TITLE>404 Not Found</TITLE> </HEAD><BODY> <H1>Not Found</H1> The requested URL $uri was not found on this server.<P> <HR> <ADDRESS>Apache/1.3.34 Server at $resuelveserver Port 80</ADDRESS> </BODY></HTML>"; //var_dump($banhosts); for ($y = 0; $y < $x; $y++) { $result = strpos($host ,$banhosts[$y]); //echo "Result for {$banhosts[$y]}: " . var_dump($result) . "\n"; if ($result !== false) { echo $notfound; //echo "Banned Host"; } // this is what i get wrong .. nothing works // elseif ($result === false) { header("location:test.php"); } // else { header("location:test.php"); } //if (!$result) { header("location:test.php"); } } ?> i can`t get it working on the redirect if $result is false to be redirected ... it keeps redirecting me directly .. Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-523810 Share on other sites More sharing options...
adv Posted April 23, 2008 Author Share Posted April 23, 2008 someone ?? Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-524735 Share on other sites More sharing options...
adv Posted April 24, 2008 Author Share Posted April 24, 2008 need of help here colegue in need Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-526501 Share on other sites More sharing options...
adv Posted April 24, 2008 Author Share Posted April 24, 2008 pff i finally done it .. thanks for the help anyway Link to comment https://forums.phpfreaks.com/topic/102054-solved-ban-hosts-problem/#findComment-526578 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.