• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Grab IP of local machine, and display code based on the ip range they are in - ASP

I manage an intranet for two remote sites - both sites need to have identical file names and folder structures.

I have location A's web server at 192.168.1.10.  All of location A is on subnet 192.168.1.x

I have location B's web server at 192.168.2.10.  All of location B is on subnet 192.168.2.x

What I would like to do is create a web page with specific information for that site, such as weather and events.  I want to detect the ip address of the machine so that the page displays the appropriate code for that site.

so the asp code would grab the ip, and say if the ip matches a sepcific subnet, display the data for that site.  Else, display the data for the other site. In very basic terms:

if ip address = 192.168.1.x range then

<html code>

else if ip address = 192.168.2.x range then

<html code>

else

<html code>

end if

any ideas?

Thanks!
0
tabascoman27
Asked:
tabascoman27
  • 6
  • 4
  • 3
2 Solutions
 
jmelikaCommented:
Hello,

Request.ServerVariable("LOCAL_ADDR") will give you the IP address of the web server.

Have fun!

JM
0
 
wesbirdCommented:
This should answer it:

<head>
</head>
<body>
<p>Server IP: <% Response.write Request.ServerVariables ("LOCAL_ADDR") %> </p>

<p>Client IP: <% Response.write Request.ServerVariables("REMOTE_ADDR") %> </p>
</body>


Wes
0
 
wesbirdCommented:
<body>
<p>Server IP: <% Response.write Request.ServerVariables ("LOCAL_ADDR") %> </p>
<p>Client IP: <% Response.write Request.ServerVariables("REMOTE_ADDR") %> </p>
<p>
<%
      strArr = split( Request.ServerVariables("REMOTE_ADDR"), "." )
      if strArr(2) = "1" then
            Response.Write "Subnet 192.168.1.x"
      else
            Response.Write "Subnet 192.168.2.x"
      end if
%>
</p>
</body>
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.

 
jmelikaCommented:
I don't personally think it's a good system to use IF in this case.  It would be better with a SELECT CASE.

<%
Select Case Request.ServerVariable("LOCAL_ADDR")
  Case "192.168.1.10"
    'Do the code of Server A
  Case "192.168.2.10"
    'Do the code of Server B
  Case Else
    'Display the result of unexpected IP address
End Select
%>

This is a better identification of the likely cases.
0
 
wesbirdCommented:
jmelika,

We're splitting hairs here - the fact is that it is Request.ServerVariables("REMOTE_ADDR") has the IP address.  We could do:

<body>
<p>Server IP: <% Response.write Request.ServerVariables ("LOCAL_ADDR") %> </p>
<p>Client IP: <% Response.write Request.ServerVariables("REMOTE_ADDR") %> </p>
<p>
<%
  select case strArr(2)
       ' ...etc would be more sensible
%>
</p>
</body>

instead.  

I'd like to wait until tabascoman27 responds please.

Cheers

Wes


0
 
wesbirdCommented:
Ooops,

plus this line from above again (P.S. jmelika is is still refering to the server IP address instead of the client one)

     strArr = split( Request.ServerVariables("REMOTE_ADDR"), "." )
0
 
jmelikaCommented:
wesbird, nice catch.  I was thinking of server's IP address because he mentioned that each site has its own server.
0
 
tabascoman27Author Commented:
Thank you for the quick responses!  I will try all today, and see what happens.  
0
 
tabascoman27Author Commented:
Well, I'm going to accept jmelika's answer.   Case worked great, and using the server variable actually works perfectly.   A laptop user from office A might come to office B to visit , but will still be pointed to his office A web server, and would still still want to see his home location's info for events, news, etc.  He'll still pick up the local DHCP address from the local subnet in office B, but still need to have office A's information.  Doing things this way also gives the opprotunity to visit the other site's web server and get information on that site while keeping the coding simple for me.

I'm going to split some points over to wesbird for an assisted answer because I might still that code somewhere else.

I have to say thank you for the quick responses, I very much appreciate it.


0
 
wesbirdCommented:
looks like the the split went wrong!
0
 
tabascoman27Author Commented:
Ah!  Sorry about that.  Clicked too fast.  I put a request in the community forum to have the question reopened so I can split.
0
 
tabascoman27Author Commented:
Increased Points
0
 
wesbirdCommented:
Thankyou
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now