I would like to read my ip address from the following page(http://l2.io/ip or other) using javascript to save him in my variable "myIp".
function getMyIP() {
var myIp;
...
return myIp;
}
How can you do?
getenv() function: The other way to get the IP address of the client is using the getenv() function. It is used to retrieve the value of an environment variable in PHP. To get the IP address of the user we need to pass the REMOTE_ADDR variable to the getenv() function. getenv("REMOTE_ADDR");
You can use RemoteAddr to get the remote client's IP address and port (the format is "IP:port"), which is the address of the original requestor or the last proxy (for example a load balancer which lives in front of your server). This is all you have for sure. This is because internally http. Header.
The networking part of the Internet is defined by exact specifications (guidelines) for connecting on the Internet. To get the client’s public IP address, JavaScript acts as a third-party server-side language. JavaScript can’t do it alone, so, jQuery is added to do this.
Learn how to get the client IP address (local and private) using only javascript. Javascript is unable to get (nor stores somewhere) the client IP, however javascript is able to create Http requests, and server side languages are able to retrieve the user public IP, so you could use this as advantage.
Let’s begin with the simplest way: applying the $_SERVER ['REMOTE_ADDR']. It returns the user IP addresses. It can be run in the following way:
However, with the introduction of the WebRTC, you'll be able to retrieve the private IP of the user with a trick using RTCPeerConnection. In this article you'll learn how to retrieve the user IP (private using pure javascript and public using a third party service) easily with a couple of tricks.
Checking your linked site, you may include a script tag passing a ?var=desiredVarName
parameter which will be set as a global variable containing the IP address:
<script type="text/javascript" src="http://l2.io/ip.js?var=myip"></script>
<!-- ^^^^ -->
<script>alert(myip);</script>
Demo
I believe I don't have to say that this can be easily spoofed (through either use of proxies or spoofed request headers), but it is worth noting in any case.
In case your page is served using the https
protocol, most browsers will block content in the same page served using the http
protocol (that includes scripts and images), so the options are rather limited. If you have < 5k hits/day, the Smart IP API can be used. For instance:
<script>
var myip;
function ip_callback(o) {
myip = o.host;
}
</script>
<script src="https://smart-ip.net/geoip-json?callback=ip_callback"></script>
<script>alert(myip);</script>
Demo
Edit: Apparently, this https
service's certificate has expired so the user would have to add an exception manually. Open its API directly to check the certificate state: https://smart-ip.net/geoip-json
The most resilient and simple way, in case you have back-end server logic, would be to simply output the requester's IP inside a <script>
tag, this way you don't need to rely on external resources. For example:
PHP:
<script>var myip = '<?php echo $_SERVER['REMOTE_ADDR']; ?>';</script>
There's also a more sturdy PHP solution (accounting for headers that are sometimes set by proxies) in this related answer.
C#:
<script>var myip = '<%= Request.UserHostAddress %>';</script>
$.ajax({
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
alert(location.ip);
}
});
This will work https too
A more reliable REST endpoint would be http://freegeoip.net/json/
Returns the ip address along with the geo-location too. Also has cross-domain requests enabled (Access-Control-Allow-Origin: *) so you don't have to code around JSONP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With