Link to home
Start Free TrialLog in
Avatar of Graeme McGilvray
Graeme McGilvrayFlag for Australia

asked on

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
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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/
Avatar of Graeme McGilvray

ASKER

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
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

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)
Sounds to me like it is working exactly as it should.
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)
No, it is not working as you want it to.  When you are on the same LAN, you do not have the external IP.
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?
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.
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?
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.
May I ask for help creating this If statement? Sort of the reason why I am here :P
I'm working on it.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad you got it working.
thanks, cheers for helping
You're welcome!