[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 82
  • Last Modified:

API giving strange outputs

Hi all, I have an API to define my external IP, however the server is local hosting the site, is giving out the SERVER WAN IP, not the client IP

    Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
    LocAPI="http://api.ipify.org/"
    oXMLHTTPLoc.Open "GET",LocAPI,False
    oXMLHTTPLoc.Send
    If oXMLHTTPLoc.Status="200" Then
      IP=oXMLHTTPLoc.responseText
      LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
	    oXMLHTTPLoc.Open "GET",LocAPI,False
	    oXMLHTTPLoc.Send
	    If oXMLHTTPLoc.Status="200" Then
		    GetTextFromUrlLoc=oXMLHTTPLoc.responseText
		    CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
        CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
	    End If
    End If	

Open in new window


The reason I have it set up like this is so that when I browse the site, it doesnt grab my LAN address (192.168.0.x)

I just would like it to grab the client IP address
0
Graeme
Asked:
Graeme
  • 11
  • 9
3 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
That code runs on the server so of course you are seeing the server IP address.  You would have to have the client connect to that location to see the client IP address.  Even then, it is not going to get your LAN IP address but your external IP address that you can see here:  http://www.whatsmyip.org/
0
 
Graemewebber4technologiesAuthor Commented:
Currently it is recording the Client IP as the server IP and locally it is currently recording my IP as the server IP

If I remove the LocAPI="http://api.ipify.org/" portion, it gets all the client IPs and my LAN IP
0
 
Dave BaldwinFixer of ProblemsCommented:
http://api.ipify.org/ returns the IP of the computer or server that connected to it.  When I click on it in your message, it returns the external IP of my computer.

I ran this version of your code on this computer and then on my Windows server on Godaddy.  On this computer it gives my client 'external' IP and location info.  On my Godaddy server, it gives the IP and location of that server.  It appears to be working correctly so I'm not sure what you think you should be getting.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Get IP ASP</title>
</head>
<body>
<h1>Get IP ASP</h1>
<%
    Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
    LocAPI="http://api.ipify.org/"
    oXMLHTTPLoc.Open "GET",LocAPI,False
    oXMLHTTPLoc.Send
    If oXMLHTTPLoc.Status="200" Then
      IP=oXMLHTTPLoc.responseText
      LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
	    oXMLHTTPLoc.Open "GET",LocAPI,False
	    oXMLHTTPLoc.Send
	    If oXMLHTTPLoc.Status="200" Then
		    GetTextFromUrlLoc=oXMLHTTPLoc.responseText
		    CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
        CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
	    End If
    End If
		Response.Write(IP & "<br>")
		Response.Write(GetTextFromUrlLoc & "<br>")
		Response.Write(CityLoc & "<br>")
		Response.Write(CountryLoc & "<br>")
 %>
</body>
</html>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Graemewebber4technologiesAuthor Commented:
Sorry for delay in response...

The issue that I am getting is, that no matter from what location anyone is access this code (on my local server in its current code form - above) it is returning the external IP that is it hosting from.

I have got friends and people from EE to test it, it picks up the Server External IP, not their External IP.

If I remove the http://api.ipify.org/ section and change IP=Server_Variable("LOCAL_ADDR") then all clients accessing the site have the correct IP, however because I am on the same LAN as the server, i picks up my LAN IP, not my External IP (which is the same as the server)
0
 
Dave BaldwinFixer of ProblemsCommented:
Sounds to me like it is working exactly as it should.
0
 
Graemewebber4technologiesAuthor Commented:
it isnt unfortunately working as it should.

currently it records the server IP instead of the remote_IP (clients IP)

when i revert it so it can record the client IP, I am it doesnt record my IP as an external, but an internal (192.168.0.x)
0
 
Dave BaldwinFixer of ProblemsCommented:
No, it is not working as you want it to.  When you are on the same LAN, you do not have the external IP.
0
 
Graemewebber4technologiesAuthor Commented:
isnt that the same as 'it isnt unfortunately working as it should.' ?

So how i get it to read my external IP on the LAN and not affect all other clients?
0
 
Dave BaldwinFixer of ProblemsCommented:
You can't as far as I know.  When you're on the LAN, the external IP address doesn't exist.  You have to use some code that goes outside the LAN to the outside world.  

Which you do have ( http://api.ipify.org/  ) but it doesn't do what you want when you run it with the other code.  When you run it by itself it gives you the external IP address.
0
 
Graemewebber4technologiesAuthor Commented:
Hmm ok

What about....a special rule... for me ?

When accessing from a 192.168.0.x address then put my external IP ?

Is that possible?
0
 
Dave BaldwinFixer of ProblemsCommented:
Sure, it's your code, you can do what you want.  I do that all the time with web sites I support to help me with troubleshooting.
0
 
Graemewebber4technologiesAuthor Commented:
May I ask for help creating this If statement? Sort of the reason why I am here :P
0
 
Dave BaldwinFixer of ProblemsCommented:
I'm working on it.
0
 
Dave BaldwinFixer of ProblemsCommented:
First, this program lists all of the variables available from the server.
<%@ LANGUAGE = VBScript %>
<%  Option Explicit		%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>ASP Server Variables</title>
</head>
<body>
<h1>ASP Server Variables</h1>
<%
Dim item

For each item in Request.ServerVariables
	Response.Write "<b>" & item & "</b>: " _
	& Request.ServerVariables(item) _
	& "<br>" & vbCrLf
Next

	Response.Write "<b>PATH_INFO</b>: " _
	& Request.ServerVariables("PATH_INFO") _
	& "<br>" & vbCrLf

%>

</body>
</html>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
This version of your code gets the external IP if the "REMOTE_ADDR" is the same as the LocalIP.  Enter your local IP as LocalIP.  You can run it here: http://www.gammaboa.net/db/Get-IP-ASP.asp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Get IP ASP</title>
</head>
<body>
<h1>Get IP ASP</h1>
<%
		LocalIP = "10.202.46.40"
    Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
		IP = Request.ServerVariables("REMOTE_ADDR")
		If IP = LocalIP Then
	    LocAPI="http://api.ipify.org/"
  	  oXMLHTTPLoc.Open "GET",LocAPI,False
    	oXMLHTTPLoc.Send
    	If oXMLHTTPLoc.Status="200" Then
      	IP=oXMLHTTPLoc.responseText
    	End If
		End If
		
    LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
	  oXMLHTTPLoc.Open "GET",LocAPI,False
	  oXMLHTTPLoc.Send
	  If oXMLHTTPLoc.Status="200" Then
		  GetTextFromUrlLoc=oXMLHTTPLoc.responseText
			If GetTextFromUrlLoc <> "{}" Then
		  	CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
      	CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
			End If
	  End If
		Response.Write(IP & "<br>")
		Response.Write(GetTextFromUrlLoc & "<br>")
		Response.Write(CityLoc & "<br>")
		Response.Write(CountryLoc & "<br>")
 %>
</body>
</html>

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
Ended up fixing it with this:
RemoteIP=Request.ServerVariables("REMOTE_ADDR")
If RemoteIP="192.168.0.x" Then
Else
End If

Open in new window

1
 
Dave BaldwinFixer of ProblemsCommented:
Glad you got it working.
1
 
Graemewebber4technologiesAuthor Commented:
thanks, cheers for helping
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome!
0
 
Graemewebber4technologiesAuthor Commented:
a
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now