Home » Php » php – Can Javascript know your IP?

php – Can Javascript know your IP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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>

Answer:

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

Answer:

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.

Answer:

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.