Retrieving Visitor IP Addresses with CloudFlare in PHP
When tracking website visitors with PHP, using $_SERVER['REMOTE_ADDR'] typically retrieves CloudFlare's IP addresses. To obtain the actual visitor IP address while using CloudFlare, consider the following approach:
CloudFlare provides additional server variables, including:
To utilize these variables, modify your code as follows:
<code class="php">if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) { $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; }</code>
By assigning the genuine visitor IP address to $_SERVER['REMOTE_ADDR'], you can accurately log and track website visitors even when CloudFlare is enabled.
Note: To ensure IP address validity, verify that $_SERVER['REMOTE_ADDR'] contains a legitimate CloudFlare IP address, as anyone can potentially spoof the header and connect directly to your server's IP.
The above is the detailed content of How to Retrieve Real Visitor IP Addresses When Using CloudFlare in PHP?. For more information, please follow other related articles on the PHP Chinese website!