Simple PHP Proxy Detection Script IP Address
Get client’s IP address in PHP
We can get the IP address of any visitor by using PHP. Finding the IP address is very important requirement for many scripts where we store the members or visitors details. For security reason we can store IP address of our visitors who are doing any purchases or doing any type of transaction online. We can using the IP address to find out geographical location of the visitor.
Sample code to get an IP address.
1 2 3 4 |
<?php $ip=$_SERVER['REMOTE_ADDR']; echo "IP address= $ip"; ?> |
This server variable: $_SERVER[“REMOTE_ADDR”] to get an IP address. However, they can be behind a proxy server in which case the proxy may have set the $_SERVER[‘HTTP_X_FORWARDED_FOR’], but this value is easily spoofed. For example, it can be set by someone without a proxy, or the IP can be an internal IP from the LAN behind the proxy.
If you use proxy. Below is a sample PHP script that can detect if a user is using a proxy or not:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $remoteaddr=$_SERVER["REMOTE_ADDR"]; $xforward= $_SERVER["HTTP_X_FORWARDED_FOR"]; if (empty($xforward)) { //user is NOT using proxy echo "You are not using proxy, your real IP address is: $remoteaddr"; } else { //user is using proxy echo "You are using a proxy, your proxy server IP is $remoteaddr while your real IP address is $xforward"; } ?> |
How to Detection Proxy use PHP Script?
Below is the standard script that can detect the real IP address of your website user with or without using a proxy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php function getRealIpAddress2() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from internet { $ipadd1=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //check ip proxy { if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) & empty($_SERVER['REMOTE_ADDR'])) //check ip proxy { $ipadd1=$_SERVER['HTTP_X_FORWARDED_FOR']; } else{ $ipadd1=$_SERVER['HTTP_X_FORWARDED_FOR'].' and your proxy address is '.$_SERVER['REMOTE_ADDR']; } } else { $ipadd1=$_SERVER['REMOTE_ADDR']; } return $ipadd1; } ?> |