Friday, September 01, 2006

How to find & evaluate client's ip using javascript

Starting this week a friend, a developer himself, asked me if I could help him to develop a logging utility or a detailed counter for his personal website. In other words he wanted to know how many web visits he accepts per day and from where.

When he saw my surprised look he added that we could only use HTML/javascript to retrieve the client ip.

I had read on the internet that you can easily get the client's ip using javascript.
Since I only have basic knowledge of javascript, I thought that it would be better to
search on the internet and find a way to do it.

All I cound find was that you can use a script like the following:

< script language="javascript">
var ip = ''
...
< /script>


Of course the above script will not work since '' tag is actually SSI( Server Side Include)and has nothing to do with javascript.

Javascript does not provide a way to get the client's ip from the http "remote_addr" header. After all Javascript lies on the client-side.
I continued seaching and finally I found out at gnucitizen that you can find the client's ip address with javascript using the following script:



< script language="javascript" type="text/javascript">
var Client.getAddressInfo = function () {
var hostname = undefined;
var address = undefined;

try {
var sock = new java.net.Socket();
sock.bind(new java.net.InetSocketAddress('0.0.0.0', 0));
sock.connect(new java.net.InetSocketAddress(document.domain, (!document.location.port)?80:document.location.port));
hostname = sock.getLocalAddress().getHostName();
address = sock.getLocalAddress().getHostAddress();
} catch (e) {}

return {hostname: hostname, address: address};
};
< /script>


The above javascript will not find the client's ip address from the header, but will get the job done in most cases.

Due to the fact that javascript was our only solution, it quickly became evident that we should send the client's ip parameter elsewhere and manipulate it there.

The next issue I thought of, was how could the hostname be evaluated before the html page executes at the client browser. Failure to evaluate the hostname before the html parsing from the browser, means that instead of sending the client's ip, the script itself would be sent as a parameter.

The solution to this problem was found at dhtmlcentral
and is as follows:


function loadContent(file){
var head = document.getElementsByTagName('head').item(0)
var scriptTag = document.getElementById('loadScript');
if(scriptTag) head.removeChild(scriptTag);
script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.id = 'loadScript';
head.appendChild(script)
}


The above function is equal as writing
< SCRIPT LANGUAGE="JavaScript" RC="hostname/scriptname">< /SCRIPT>.

So if we execute a function that generates the SRC url and pass that function as a parameter to loadContent, we could dynamically invoke the specific url we want to use while sending the parameter we want at the same time.

The following example:


function gethost(){
return "http://somehost.com/pathTo/webPage?clientip="+Client.getAddressInfo().address;

};
loadContent(gethost());

would get the job done.

Finally, the joy of completion. Now If we could only find a way to convert that ip into country names we could generate various interesting statistics.

For example, if we were a company and we saw that certain percentage of our visits comes from a specific country, then we could reasses our overall strategy or our strategy regarding that country.

A possible way of converting the hostname into coutry name using PHP can be found at weberdev

9 comments:

Anonymous said...

thank you very much!

find your ip address said...

Thanks a lot I've been looking for how to do this with IP's and javascript.

HarshReality said...

OK, so can you lay out a simple php script (beginning to end) on this function. Id be greatly appreciated.

HarshReality said...

Can somebody give me an example for loading this using php and calling back the data?

HarshReality said...

Can you provide a full example in a php file format that could for example be used as an include?

Anonymous said...

I just would like to make it clear that Java objects are supported only when using JavScript on Safari or Opera.

No such support for IE.

Thanks for the post.

Mikhus said...

WAn easy way:

$(document).ready( function() {
$.getJSON( "http://smart-ip.net/geoip-json?callback=?",
function(data){
alert( data.host);
}
);
});

man9ar00 said...

Just wanted to point out that doesn't appear to be a pure Javascript solution. It looks like the Javascript is calling Java code (e.g. java.net.socket) to get the hostname and IP. Have you or anyone actually tested this across browsers? Would be interested to know which browsers it will work on and which won't. Along with whether you need to load some Java applet, etc. to get this to work.

nanoagent said...

Mikhus than you for your reply is intentersting to see how thing s change thru years eh :) what could be done not "so easy" 7 years ago can be done in 6 lines of code easily today :) ty for your addition , good one :)

 
Arcane Palette Creative Design
blogger template by arcane palette