We help IT Professionals succeed at work.

looking for php redirect based on ip address

BCLS Tech
BCLS Tech asked
on
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.
Comment
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
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

Author

Commented:
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
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
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

Author

Commented:
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.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
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.

Author

Commented:
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.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Sure, the ASP script would have the details you're talking about.

Author

Commented:
There are 2 asp files.  Files are attached.

Thanks for your help.


1.txt
2.txt
Fixer of Problems
Most Valuable Expert 2014
Commented:
This should do exactly what 1.txt does.  Let me know if you have a problem.  Make sure and save this as 'index.php' at the point where you need the redirect.
<?php 
	$rmtip = $_SERVER['REMOTE_ADDR'];
	if ((substr($rmtip,0,2) == "10") || (substr($rmtip,0,10) == "205.152.65")) header('Location: http://www.viamicro.com/search/databases.htm');
	else header('Location: http://205.152.65.243/rpa/webauth.exe?tm=libselect');
	exit;
?>

Open in new window

Author

Commented:
Worked flawlessly.

Author

Commented:
Thank you!