How to Retrieve Real Visitor IP Addresses When Using CloudFlare in PHP?

Patricia Arquette
Release: 2024-10-25 04:37:30
Original
335 people have browsed it

How to Retrieve Real Visitor IP Addresses When Using CloudFlare in PHP?

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:

  • $_SERVER["HTTP_CF_CONNECTING_IP"]: The actual visitor IP address
  • $_SERVER["HTTP_CF_IPCOUNTRY"]: Visitor's country code
  • $_SERVER["HTTP_CF_RAY"]: Unique identifier for the request
  • $_SERVER["HTTP_CF_VISITOR"]: Indicates if the request is over HTTP or HTTPS

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!