Grab IP Address with JavaScript
Is it possible to grab a client side IP Address using JavaScript in an htm
page? I have a web page hosted on a non-php server and I want a piece of
code in JavaScript to grab the IP address of the client and send it to a web
page on another server or emailing the ip address to me. The other page will be php.
I need this for both Netscape and IE browsers.
Any ideas?
Re: Grab IP Address with JavaScript
Does the Web server run any server side scripts? ASP for example?
Re: Grab IP Address with JavaScript
Quote:
Originally Posted by tony007
Is it possible to grab a client side IP Address using JavaScript in an htm page?
Maybe, not sure, but...
Quote:
Originally Posted by tony007
I want a piece of
code in JavaScript to grab the IP address of the client and send it to a web
page on another server
Not possible, JS HTTP requests can only be made to the domain the page is served from.
Quote:
Originally Posted by tony007
or emailing the ip address to me.
Also not possible.
Re: Grab IP Address with JavaScript
Quote:
Originally Posted by rory
Does the Web server run any server side scripts? ASP for example?
Thank u for u reply. The one that has the html page does not support any scripting but the other supports php and both are in freee webhosting. I be happy if u show me how to get ip of client upon vision and send it to myemial or send it webserver that supports php for inserting it to mysql or email it some how. Thanks
Re: Grab IP Address with JavaScript
If you have Server Side Includes (SSI/SHTML) then you can do:
HTML Code:
<script type="text/javascript">
var ip = '<!--#echo var=REMOTE_ADDR -->';
alert('Your IP is ' + ip);
</script>
What happens here is that before the page is sent by the server, the SSI parses the page, finds the #echo statement and replaces the HTML comment with IP address of the client. Thus JavaScript on the client machine is like var ip = '0.0.0.0';
Most often SSI must be enabled separately for .htm and .html pages, this can be done using .htaccess. Or just use .shtml.
Re: Grab IP Address with JavaScript
i dont know anything bout Php but you could use an img tag to run a remote script .. cant say for sure 100% if it will get the IP though ...
<img src="http://www.someurl.com/mypage.php" border="0" with="1" height="1">
Re: Grab IP Address with JavaScript
another option ..
<SCRIPT>
var ip = new java.net.InetAddress.getLocalHost();
var ipStr = new java.lang.String(ip);
document.writeln(ipStr.substring(ipStr.indexOf("/")+1));
</SCRIPT>
Re: Grab IP Address with JavaScript
That returns the internal (LAN) IP, not the remote address.
Additionally, it takes an eternity to run.
Re: Grab IP Address with JavaScript
Quote:
Originally Posted by penagate
That returns the internal (LAN) IP, not the remote address.
Additionally, it takes an eternity to run.
oh well .. blame google .. :D