Ban IP's

Hi all,

I had a previous question here before on banning certain IPs and redirect them to another page

I got the following code


<?php
/* put this at the top of your page */
$certain_ips = array('62.215','62.150','195.226','195.39','213.189','213.181','168.187','194.54');
foreach($certain_ips as $ip)
{
 if(substr($_SERVER['REMOTE_ADDR'], 0, strlen($ip)) == $ip)
 {
  header('Location:login.php');
  exit();
 }
else
{
header('Location:chat.php');
exit();
}
}
?>


The IPs shown are the banned IPs in my site. The problem with this code that it bans only the first two IPs but the rest still can access the site normally.

Anyone can fix that plz?

SouledAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Diablo84Commented:
$certain_ips[8]

should be

$certain_ips

and by the looks of it you should cut out the else clause, ie:

/* put this at the top of your page */
$certain_ips = array('62.215','62.150','195.226','195.39','213.189','213.181','168.187','194.54');
foreach($certain_ips as $ip)
{
 if(substr($_SERVER['REMOTE_ADDR'], 0, strlen($ip)) == $ip)
 {
  header('Location:login.php');
  exit();
 }
}

header('Location:chat.php');
exit();

}

otherwise for everyone of the ip's its going to redirect to one or the other pages, if the ip doesnt match then it will automatically load the other page. With the above it will check all of the ips then if none of them are banned it will redirect to chat.php, not sure if thats the expected behaviour but thats how i read into it.

failing that did you try the alternative code i posted in your previous question?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Diablo84Commented:
sorry that last } should not be there
0
SouledAuthor Commented:

I noticed that.
It gave me error when I processed it.

Now its all fine.

Thanks man

0
Diablo84Commented:
no problem :)
0
SouledAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.