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() Quote Link to comment 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; } } ?> Quote Link to comment 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 Quote Link to comment 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(); Quote Link to comment 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 ?!?! Quote Link to comment Share on other sites More sharing options...
adv Posted April 22, 2008 Author Share Posted April 22, 2008 anyone!?!? Quote Link to comment 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"; } } ?> Quote Link to comment 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 .. Quote Link to comment Share on other sites More sharing options...
adv Posted April 23, 2008 Author Share Posted April 23, 2008 someone ?? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.