To extract IP Address of any real Website visitor or client using PHP , We should have some basic idea about predefined variables that are used in PHP. The predefined variables in PHP are Superglobal , which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods. Under this predefined variables we need look at $_SERVER['___'] variable to get IP Address .
$_SERVER is an array containing information such as headers, paths and script locations.The entries in this array are created by the web server .
You can get more details of $_SERVER : http://php.net/manual/en/reserved.variables.server.php
$_SERVER is an array containing information such as headers, paths and script locations.The entries in this array are created by the web server .
You can get more details of $_SERVER : http://php.net/manual/en/reserved.variables.server.php
How to extract IP Address of the Visitor ?
IP address of the Client or user who viewed your Website can be got by using $_SERVER['REMOTE_ADDR'] . This will Print the IP Address of the Visitor (viewer) who requested the webpage to the Server . So when the request is made to the Server ,It stores the IP Address in $_Server Global variable which can be accessed using $_SERVER['REMOTE_ADDR'] variable .
<?php
$ipaddress = '';
if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
echo "\n Your IP Address is :\t".$ipaddress;
?>
You Can view the Demo here : Codispatch Demo
How to extract Real IP address if the Visitor is using Proxy Server ?
Some times visitor can spoof or hide the actual IP address that is assigned to that user and use Proxy Server address instead. This is commonly done using Hidemyass Proxy List . Now we can't get the actual IP address of the visitor using PHP but we do can detect if it is Proxy Server or not, this is done by
<?php
$realipaddress = '';
if ( $_SERVER['HTTP_X_FORWARDED_FOR'] || $_SERVER['HTTP_X_FORWARDED'] || $_SERVER['HTTP_FORWARDED_FOR'] || $_SERVER['HTTP_CLIENT_IP'] || $_SERVER['HTTP_VIA'] || in_array($_SERVER['REMOTE_PORT'], array(8080,80,6588,8000,3128,553,554)) || @fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 30))
{
$realipaddress = "Proxy detected \t".$_SERVER['REMOTE_ADDR'];
echo $realipaddress;
}
?>
You can view the Demo here : Codispatch Demo
(Note that you must use or spoof some Proxy server address in-order to understand the above demo )
Now you know how to extract the IP address of the Visitor even if the user is using Proxy Server .If you have any questions or suggestions please comment it below .
Thanks for sharing such types of articles on your website. I have read the complete article that you have explained properly. Thanks a lot for this article. For any kind of website development in the PHP Language, visit our website.
ReplyDeletevisit our website