Jump to content

[SOLVED] ban hosts problem


adv

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.