[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Redirect certain IP's

Posted on 2004-10-29
8
Medium Priority
?
274 Views
Last Modified: 2008-02-26
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.


0
Comment
Question by:Souled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12449668
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
 

Author Comment

by:Souled
ID: 12449705

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
 
LVL 49

Expert Comment

by:Roonaan
ID: 12449728
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Souled
ID: 12449742
How can I specify the banned IPs in the last code?

0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 12449762
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12449782
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
 

Author Comment

by:Souled
ID: 12449847


it worked just fine THANKS ALOT MAN


u earned it :)


0
 

Author Comment

by:Souled
ID: 12454115
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question