Redirect certain IP's

Hi all,

I need to redirect a certain IP's to a specific login page.
for example:
IP: 62.150.X.X
redirected to login.php

The reason I need this code is that I would like to redirect a certain country to a login page on my chat site, other countries have direct access to the chat service without providing username/password.

One more thing, I have around 10 IP's to redirect.


SouledAsked:
Who is Participating?
 
RoonaanCommented:
The array $certain_ips contain the leftmost parts of the addresses that need to be redirected.

The array
$certain_ips = array('62.150','213.189');
Would ban
62.150.*.* and 213.189.*.*

-r-

0
 
RoonaanCommented:
Try the snippet below:

<?php
/* put this at the top of your page */
$certain_ips = array('62.150.0.1','62.150.0.2');
if(in_array($_SERVER['REMOTE_ADDR'], $certain_ips))
{
header('Location:login.php');
exit();
}
?>

Regards

-r-
0
 
SouledAuthor Commented:

I have tried it and it worked fine.

one thing has to be changed though. The form which the IPs r entered here is static.
What i meant 62.150.X.X is that we need to use only 62.150 and any other possible class following without entering them manually.

I have tried to change this line to the following form

$certain_ips = array('62.150','213.189');

but it did not work.

any solution for that?

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RoonaanCommented:
Well, you could use:

foreach($certain_ips as $ip)
{
 if(substr($_SERVER['REMOTE_ADDR'], 0, strlen($ip)) == $ip)
 {
  header('Location:login.php');
  exit();
 }
}

An alternative would be a preg match, but the solution above is somewhat more maintainable in my opinion.

-r-
0
 
SouledAuthor Commented:
How can I specify the banned IPs in the last code?

0
 
Diablo84Commented:
A regex example derived from an IP blocking script I had in action on a site i built some time back:

$ip[] = '^([6][2](\.[1][5][0])(\.[0-9]{1,3}){2})$';

//some other examples of use, you can remove this block of code if you don't need it for reference
//$ip[] = '111.111.111.111';
//$ip[] = '^([1][1][1](\.[0-9]{1,3}){3})$';  //BAN 111.*
//$ip[] = '^([1][1][1](\.[1][1][1])(\.[0-9]{1,3}){2})$';  //BAN111.111.*
//$ip[] = '^([1][1][1](\.[1][1][1])(\.[1][1][1])(\.[0-9]{1,3}))$';  //BAN 111.111.111.*
//$ip[] = '^([0-9]{1,3}(\.[0-9]{1,3}){3})$';  //to ban ALL

$user_ip = $_SERVER['REMOTE_ADDR'];

foreach($ip as $var) {
 if(ereg($var,$user_ip)){
  header("location: login.php");
  exit;
 }
}
0
 
SouledAuthor Commented:


it worked just fine THANKS ALOT MAN


u earned it :)


0
 
SouledAuthor Commented:
Roonaan

I know had accepted the code already
but I have a small problem with it now

I have a list of 10 banned IPs. The code provided by you bans only the first two in the array.

Could you fix that? I can put this question as new ticket if you wish
0
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.

All Courses

From novice to tech pro — start learning today.