Link to home
Start Free TrialLog in
Avatar of BCLS Tech
BCLS TechFlag for United States of America

asked on

looking for php redirect based on ip address

I am looking for a php script that will determine a users ip address and send them to a particular url.
For example.

If users IP starts with 10 then send to this URL1
If users IP starts with 192.168.1 then send to URL1
Else
send the user to URL2

Any help would be greatly appreciated.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

This should do it.
<?php 
	$rmtip = $_SERVER['REMOTE_ADDR'];
	if ((substr($rmtip,0,2) == "10") || (substr($rmtip,0,9) == "192.168.1")) header('Location: http://www.URL1.com/');
	else header('Location: http://www.URL2.com/');
	exit;
?>

Open in new window

Avatar of BCLS Tech

ASKER

Works! Sort of...

Can another condition be added for URL 1?

If users IP starts  with 10 then send to this URL1
If users IP starts with 192.168.1 then  send to URL1
IF users IP starts with 172.168.1 then send to URL 1
Else
send the user to URL2
I think you meant '172.16.1'...  172.168.1.x is not a private IP block so wouldn't be found on an internal LAN.
<?php 
	$rmtip = $_SERVER['REMOTE_ADDR'];
	if ((substr($rmtip,0,2) == "10") || (substr($rmtip,0,9) == "192.168.1") || (substr($rmtip,0,8) == "172.16.1")) header('Location: http://www.URL1.com/');
	else header('Location: http://www.URL2.com/');
	exit;
?>

Open in new window

We have both public and private IP's.
Is there a way to get our private and public IP blocks to point to URL1 but all other public ip blocks to default to the Else?

 Hope I said that right.
Well, just do more of the same.  Is that code doing what you want so far?  There are also methods of redirecting on the server itself.  If you're interested in that, you should 'Request Attention' and get your server's zone added to this question.
10 is working and taking me to the internal site


Our systems with public IP's do not seem to be working.

If I can find it, would it help If I sent you the ASP script that was on our old server?
It was doing the job until the new server and new site was put online.  Problem is we don't have asp on the new server.
Sure, the ASP script would have the details you're talking about.
There are 2 asp files.  Files are attached.

Thanks for your help.


1.txt
2.txt
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked flawlessly.
Thank you!