Solved

API giving strange outputs

Posted on 2016-10-16
20
24 Views
Last Modified: 2016-11-02
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
Comment
Question by:Graeme
  • 11
  • 9
20 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41846179
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
 

Author Comment

by:Graeme
ID: 41846187
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41846231
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
 

Author Comment

by:Graeme
ID: 41860811
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41860847
Sounds to me like it is working exactly as it should.
0
 

Author Comment

by:Graeme
ID: 41861619
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41861654
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
 

Author Comment

by:Graeme
ID: 41861660
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41861758
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
 

Author Comment

by:Graeme
ID: 41861763
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41861808
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
 

Author Comment

by:Graeme
ID: 41861830
May I ask for help creating this If statement? Sort of the reason why I am here :P
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41863141
I'm working on it.
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 500 total points
ID: 41863190
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 500 total points
ID: 41863206
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
 

Accepted Solution

by:
Graeme earned 0 total points
ID: 41864869
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41864896
Glad you got it working.
1
 

Author Comment

by:Graeme
ID: 41864897
thanks, cheers for helping
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41864901
You're welcome!
0
 

Author Closing Comment

by:Graeme
ID: 41869863
a
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now