7

Possible Duplicate:
Get Client IP using just Javascript?

I know PhP can know your IP from <?php echo $_SERVER['REMOTE_ADDR']; ?>

How can javascript do so?

Or is it true that because javascript is client side it doesn't know the IP of the computer?

Community
  • 1
  • 1
user4951
  • 32,206
  • 53
  • 172
  • 282
  • Which IP of the computer? I have 7 on the machine I'm working on right now. – Quentin Jul 19 '11 at 10:48
  • I don't think it's an exact duplicate to the question given above. Here, PHP may be used (as the question is tagged with PHP) in the other question server side code is explicitly undesired. – Martin Jul 19 '11 at 12:20

4 Answers4

4

JavaScript is executed in your browser, it can't know your IP.
But you can get IP from back-end with AJAX

wong2
  • 34,358
  • 48
  • 134
  • 179
4

That's correct. There's no way to confirm your public IP without connecting to another server, so it's not directly available in client-side JavaScript.

Jeremy
  • 1
  • 85
  • 340
  • 366
4

If the file containing your JS code is parsed by PHP, you should be able to inject the IP-address into the JS code by PHP.

<script type="text/javascript">
 var ipaddress = "<?php echo $_SERVER['REMOTE_ADDR']; ?>";
</script>
Martin
  • 1,889
  • 3
  • 20
  • 25
1

You can use an HTTP request to call an existing API which provides the IP. For example there is the XML API at domaintools.com, but there should be much more simpler APIs to access.

schlamar
  • 9,238
  • 3
  • 38
  • 76