Current location: Hot Scripts Forums » Programming Languages » ASP » Response.Redirect based on IP address


Response.Redirect based on IP address

Reply
  #1 (permalink)  
Old 08-20-08, 04:44 PM
zarc zarc is offline
New Member
 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Response.Redirect based on IP address

Hi all

Please help with the correct ASP code to achieve the following:

Want to redirect all visitors landing on page "test.asp" to http:mytest.com/testing.asp except a single visitor from IP address xxx.xxx.xx.x

If vistor from IP xxx.xxx.xx.x then do not redirect, just display test.asp like normal

If visitor from any other IP then redirect to http:mytest.com/testing.asp

Thanks for your help

Cheers
Reply With Quote
  #2 (permalink)  
Old 08-20-08, 09:47 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Try it like this:

test.asp
ASP Code:
  1. <%
  2. allowed_addr = "xxx.xxx.xx.x"
  3.  
  4. if Request.ServerVariables("remote_addr") <> allowed_addr then
  5. response.Redirect("http://mytest.com/testing.asp")
  6. else
  7. %>
  8.  
  9. <html>
  10. <head>
  11. <title></title>
  12. </head>
  13. <body>
  14. <div>This is the normal page for test.asp</div>
  15. </body>
  16. </html>
  17.  
  18. <%
  19. end if
  20. %>
__________________
Jerry Broughton

Last edited by job0107; 08-20-08 at 09:52 PM.
Reply With Quote
  #3 (permalink)  
Old 08-21-08, 04:04 AM
zarc zarc is offline
New Member
 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Jerry, works perfectly.

Thanks for your advice.

Cheers
Craig
Reply With Quote
  #4 (permalink)  
Old 08-22-08, 03:51 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It's usually a good practice to add response.end after each response.Redirect() call.
Reply With Quote
  #5 (permalink)  
Old 09-22-11, 11:33 AM
Jordy Jordy is offline
New Member
 
Join Date: Sep 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Very cool... but..

Hi there,

I saw this script and thought it was awesome, but is there anyway to send the rest of the IP ranges to another page, rather than loading test.asp?

So basically you never load test.asp, you simply get forwarded on depending on your IP range.

Look forward to hearing from someone.

Jordy
Reply With Quote
  #6 (permalink)  
Old 09-22-11, 05:34 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by Jordy View Post
Hi there,

I saw this script and thought it was awesome, but is there anyway to send the rest of the IP ranges to another page, rather than loading test.asp?

So basically you never load test.asp, you simply get forwarded on depending on your IP range.

Look forward to hearing from someone.

Jordy
I suppose you could load your IP ranges into a SESSION variable and use it on any page you like.
__________________
Jerry Broughton
Reply With Quote
  #7 (permalink)  
Old 09-23-11, 02:38 AM
Jordy Jordy is offline
New Member
 
Join Date: Sep 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
I suppose you could load your IP ranges into a SESSION variable and use it on any page you like.
Oh okay, I'm quite knew to this, so how would I go about doing that? Glad to see there is a possible solution though. look forward to hearing from you.
Reply With Quote
  #8 (permalink)  
Old 09-23-11, 07:32 AM
Jordy Jordy is offline
New Member
 
Join Date: Sep 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Okay been fiddling around with this on various browsers and noticed an issue.

If you are picking up the correct IP range it's looking for.. say, 10.0.0.0 you get forwarded on to the correct page. However if you don't have the correct IP range, it isn't ignoring the redirect and loading the page text :

"This is the normal page for index.asp"

Its still trying to find the redirector address?

Now, I was thinking, if I can get it to load the body of test.asp then I could add a simple JS script to redirect those people to another page.

Any ideas how to get the page to load if the above IP range isn't detected?

Look forward to hearing from someone

Richard
Reply With Quote
  #9 (permalink)  
Old 09-24-11, 12:32 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Forget what I said before.
The script you were looking at here is looking to see if a specific IP address is detected,
if it is, then the page will be displayed and all other IP addresses will be redirected to a different page.
If you want to detect a range of IP addresses you could use a Select Case statement and specify as many IP addresses you want using several Case statements.
For instance, lets say you have a small network that share one server and each client computer is assigned it's own IP address.
Let's say your network has 5 computers:
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
When one of the computers on your network accesses your site, content is displayed for that computer.
If a computer that is not part of your network accesses your site, then some other content is displayed.
Example:
Code:
<%
Select Case Request.ServerVariables("remote_addr")
         Case "127.0.0.1"
                page = "<div>This is the page for computer 1</div>"
         Case "127.0.0.2"
                page = "<div>This is the page for computer 2</div>"
         Case "127.0.0.3"
                page = "<div>This is the page for computer 3</div>"
         Case "127.0.0.4"
                page = "<div>This is the page for computer 4</div>"
         Case "127.0.0.5" 
                page = "<div>This is the page for computer 5</div>"
         Case Else
                page = "<div>This is the page for everybody else</div>"
End Select
%>
<html>
<head>
<title></title>
</head>
<body>
<%
response.write page
%>
</body>
</html>
You could also put the different content in to separate pages and use the response.redirect command.
Example:
Code:
<%
Select Case Request.ServerVariables("remote_addr")
         Case "127.0.0.1"
                page = "page1.html"
         Case "127.0.0.2"
                page = "page2.html"
         Case "127.0.0.3"
                page = "page3.html"
         Case "127.0.0.4"
                page = "page4.html"
         Case "127.0.0.5" 
                page = "page5.html"
         Case Else
                page = "page6.html"
End Select
response.redirect(page)
response.end
%>
__________________
Jerry Broughton
Reply With Quote
  #10 (permalink)  
Old 09-26-11, 04:02 AM
Jordy Jordy is offline
New Member
 
Join Date: Sep 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Hi there,

thank you so much for your reply and indeed, that was the way I thought it would work, but my task is a little more complicated.

Essentially this is what I have to do. I need to host a page on the world wide web that detects the IP range of all our users and redirects them accordingly. To identity our users we need to check for our Subnet IP address - essentially the network address.

What is needs to do is point everyone using our VPN or internal to our building (that has a subnet address in the 10.0.0.0 range) and point them to our intranet page, hosted at say http://intranet (if you are in the building or using the VPN) and everyone one else, not on our VPN and external to our building to say Google. This is for all our remote users that will have our Intranet address set as their browsers homepage.

So while it's possible to list every IP address on our subnet, it would take me a year to list them all so hence the need to cover the entire range in one look up.

I'm sure it is possible, as I would imagine this is how country detection is done, over a range of IPs. That's what the very first script at the top of this post seemed to work as it was identifying our subnet/network address.

Thank you so much for your help thus far, all of this is really helpful and the tests I'm running through are helping me understand how this thing works. I'm no developer and am just trying to do the best I can and any help is fantastic support.

Look forward to hearing soon.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
WHM IP Address error cancer10 Web Servers 4 12-07-06 08:40 PM
Ip Address Question? purpleman The Lounge 2 07-12-06 05:59 PM
Refresh IP Address thorchan Visual Basic 0 04-16-05 02:30 AM
Regular Expression IP Address Replacement CMIVXX PHP 3 11-20-04 07:27 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 04:01 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.