Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user ip with jquery

I want to get the user's IP address with jQuery or JavaScript, it doesn't really matter but I prefer jQuery.

I've seen some answers here but they didn't work for me.

like image 377
andrei Avatar asked Jul 26 '26 06:07

andrei


2 Answers

This has been taken from How to get client IP address using jQuery

$.getJSON("http://jsonip.appspot.com?callback=?",
    function(data){
       alert( "Your ip: " + data.ip);
  });
like image 105
jams Avatar answered Jul 28 '26 21:07

jams


That isn't exposed to javascript, but if you really need it you could spit it out in your markup (assuming php):

<head>
<meta name="ip" content="<?php echo $_SERVER["REMOTE_ADDR"] ?>">
–
<script>$(function(){ alert( $("meta[name=ip]").attr("content") ) })</script>

Not sure why you would want to though, javascript runs on the client, so when would you need its ip? From javascript's perspective, it is safe to assume that the user's ip is 127.0.0.1.

like image 40
Sam Coles Avatar answered Jul 28 '26 21:07

Sam Coles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!