How can I get the client IP using PHP?
If you are making a request from the machine where the server is running, then $_SERVER['REMOTE_ADDR']
will obviously contain your own IP address, which is 192.168.0.1
Get client IP address via third party web service
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.
HTTPS support
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
With back-end logic
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>
How can I query the IP address of subscribers?
function get_client_ip()
{
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
Source
Related Topics
How to Get Beanstalkd Queue to Work for PHP
Combinations, Dispositions and Permutations in PHP
Mechanisms for Tracking Db Schema Changes
Yosemite/El Capitan PHP-Gd + Mcrypt Installation
How to Read Large Worksheets from Large Excel Files (27Mb+) with PHPexcel
Can You Add an If Statement in Order By
PHP Creating Zips Without Path to Files Inside the Zip
PHP Send E-Mail with Attachment
How to Check If There Is a There Is a Wget Instance Running
How to Get the Character from Unicode Code Point in PHP
How to Implement Pagination in PHP
How to Convert String to Boolean PHP
Laravel Eloquent Sum of Relation's Column
How to Get Ssl Certificate Info with Curl in PHP