trying to redirect based on ip address without success??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techexpressinc
Forum Newbie
Posts: 21
Joined: Wed Aug 06, 2008 10:58 am

trying to redirect based on ip address without success??

Post by techexpressinc »

Objective is to redirect the website user based on the location. If the user is at the office at a specified ip address, when an icon is clicked they will have access to the employee area. If the user is not at the specified ip address they will get a not access web page.
A icon on the home page will be an hyperlink to the following PHP code:
( ipfliper.php)

Code: Select all

<?php
if ($_SERVER['REMOTE_ADDR']=='69.245.218.248') { header('Location: http://www.turnstone.org/employee2/index.html');}
 else
 { header('Location: http://www.turnstone.org/notatoffice.html');}
exit;
?><?php
if ($_SERVER['REMOTE_ADDR']=='69.245.218.248') { header('Location: http://www.turnstone.org/employee2/index.html');}
 else
 { header('Location: http://www.turnstone.org/notatoffice.html');}
exit;
?>
I keep trying without success? Any ideas?
Thank you Russ at Techexpressinc.com
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: trying to redirect based on ip address without success??

Post by Eric! »

Besides having a second set of code that is redundant, they should at least work. Have you tried echoing the IP that the server thinks it is seeing?
User avatar
techexpressinc
Forum Newbie
Posts: 21
Joined: Wed Aug 06, 2008 10:58 am

Re: trying to redirect based on ip address without success??

Post by techexpressinc »

i am using this to see the ip

Using this on index2.html to get ip.
<p><script language="JavaScript">
VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/visitor ... "></script>
User avatar
techexpressinc
Forum Newbie
Posts: 21
Joined: Wed Aug 06, 2008 10:58 am

Re: trying to redirect based on ip address without success??

Post by techexpressinc »

my code to identify the ip is at

http://www.turnstone.org/index2.html

any ideas of where i am going wrong? :oops:
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: trying to redirect based on ip address without success??

Post by AlanG »

Code: Select all

<?php
if($_SERVER['REMOTE_ADDR'] == '69.245.218.248') {
    header('Location: http://www.turnstone.org/employee2/index.html');
    exit();
}
else {
    header('Location: http://www.turnstone.org/notatoffice.html');
    exit();
}
?>
I assume you have a static IP address at the office? Otherwise this code won't always work as the IP address won't always be 69.245.218.248.
The IP addresss you have on the home page is fetched using JavaScript and shouldn't interfere with this code at all.

You mentioned there is a link to this code. In that case, the user won't be redirected to the proper url until they click that link. If you want to implement this code, you should copy and paste it to whichever page requires it, or use a php include <?php include('ipfliper.php'); ?>
User avatar
techexpressinc
Forum Newbie
Posts: 21
Joined: Wed Aug 06, 2008 10:58 am

Re: trying to redirect based on ip address without success??

Post by techexpressinc »

I installed the new ipfliper code you suggested.
Checked my ip at http://www.turnstone.org/index2.html - bottom of the page
It still displayed
You are visiting from:
IP Address: 69.245.218.248
Host: c-69-245-218-248.hsd1.in.comcast.net

when i executed ipfliper the screen displayed was
Not a office:
http://www.turnstone.org/notatoffice.html

I wanted the
employee/index.html

Thanks for the help, but any more good ideas??
Russ

l
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: trying to redirect based on ip address without success??

Post by AlanG »

Do an <?php echo $_SERVER['REMOTE_ADDR']; ?> and tell us what's displayed.
User avatar
techexpressinc
Forum Newbie
Posts: 21
Joined: Wed Aug 06, 2008 10:58 am

Re: trying to redirect based on ip address - not working

Post by techexpressinc »

I did get an ip. I put that ip in my php code and it directed you to the good ip url no matter what computer/location you were at. So that must be like the host server.
Any more ideas on this pbl.
Thx
Russ
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: trying to redirect based on ip address without success??

Post by Eric! »

If your location and computer is changing but your IP isn't then you must be using a shared host provider for the other locations. It is hard to understand your explanations of your results too. What is the good ip url? Is the bad one notatoffice?

If you echo back the IP address on both the pages (good url and bad url) you will be able to see if your IP is changing. If you're accessing this server from computers in the same building, odds are good you're using a shared IP. If your "office computer" is using dynamic IP's then your IP will keep changing and your IP filter won't work.
Post Reply