Obtaining the connecting clients IP address (Real IP) in PHP

PHP supports HTTP_X_FORWARDED_FOR and HTTP_X_REAL_IP header through the definitions within the .PHP web files. The definitions are usually entered in the web pages configuration files. To enable HTTP_X_FORWARDED_FOR and HTTP_X_REAL_IP forwarding for the retrieval of the connecting users IP address.

Edit a PHP file that is opened when people visit your website. So, configuration files are opened every time a user visits. These files often named: config.php, global.php or defines.php

 

1. Open the PHP file in the text editor.


2. At the top of the file after <?php, add:
 

<?php

function ipCheck() {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}

elseif (getenv('HTTP_X_REAL_IP')) {
$ip = getenv('HTTP_X_REAL_IP');
}

else {
$ip = $_SERVER['REMOTE_ADDR'];
}

return $ip;
}
echo ipCheck();
?>


This configures your script to look for the HTTP_X_FORWARDED_FOR and HTTP_X_REAL_IP header, and uses that as the clients IP address instead of your proxy IP. 

If successful the IP of users connecting through the proxy service should be visible in the script you are using on your website.

Have more questions? Submit a request

Comments

  • Avatar
    voip calplud

    ns-1229.awsdns-25.org demo for SMS pstGdd.com free serve r

Powered by Zendesk