Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

API giving strange outputs

Posted on 2016-10-16
20
Medium Priority
?
76 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
20 Comments
 
LVL 84

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 84

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 84

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 84

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 84

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

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 84

Expert Comment

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

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 2000 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 2000 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 84

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 84

Expert Comment

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

Author Closing Comment

by:Graeme
ID: 41869863
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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