Results 1 to 9 of 9

Thread: Grab IP Address with JavaScript

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    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?

  2. #2
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Grab IP Address with JavaScript

    Does the Web server run any server side scripts? ASP for example?

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    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

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.
    Last edited by Merri; Jul 27th, 2006 at 06:51 AM.

  6. #6
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

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

  7. #7
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    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>

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Grab IP Address with JavaScript

    That returns the internal (LAN) IP, not the remote address.

    Additionally, it takes an eternity to run.

  9. #9
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width