faqts : Computers : Programming : Languages : JavaScript : Browser Settings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

73 of 114 people (64%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

Can I read the ip address of the client the script is run on?
Can I read the host adress of the client the script is run on?

Jan 25th, 2001 03:20
Martin Honnen,


NN4 (and NN3 I think but I haven't checked the code with it) allows 
that by calling into Java:
  var localhost = java.net.InetAddress.getLocalHost();
  var hostname = localhost.getHostName();
  var hostIP = localhost.getHostAddress();

  alert('You are visiting from host ' + hostname  
        + ' with ip address ' + hostIP);

The only way with IE to use Java is an applet but the above code 
(transcribed into Java) run in an applet is reported to return
  localhost
for the hostname respectively
  127.0.0.1
for the ip address.

So all you can do for IE (and Opera and NN6) is to use server side 
assists meaning a cgi script or php script dynmically returning the 
REMOTE_ADDR cgi variable. I have set up such an assists at
  http://liberty.datablocks.net/jshelper/ipAddress.php
which returns a statement of the form
  var ipAddress = '123.123.123.123;
so that a script can then access the variable
  ipAddress
to read the value e.g.

<HTML>
<HEAD>
<SCRIPT SRC="http://liberty.datablocks.net/jshelper/ipAddress.php">
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
document.write(ipAddress)
</SCRIPT>
</BODY>
</HTML>