Home > Article > Backend Development > How to Get the Real IP Address of Visitors Using PHP, Even When They Are Using Proxies?
Overcome Proxy Obfuscation: Unveiling Visitors' Real IP Addresses
As web developers, we often encounter the challenge of obtaining a visitor's true IP address, especially when they utilize proxy services to conceal their identity. This article will provide a solution to this problem by showcasing a robust PHP code that effectively retrieves the visitor's actual IP address, even in the presence of proxy servers.
The provided code begins by inspecting the HTTP_CF_CONNECTING_IP server variable, which is commonly set by CloudFlare when using their proxy service. If this variable is present, the code updates the HTTP_CLIENT_IP, REMOTE_ADDR, and assigns the value of HTTP_CF_CONNECTING_IP to each.
Following this, the code examines three primary server variables: HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR, and REMOTE_ADDR. It prioritizes HTTP_CLIENT_IP as the most reliable. If HTTP_CLIENT_IP is a valid IP address, it is selected as the final IP address.
If HTTP_CLIENT_IP is unavailable or invalid, the code proceeds to inspect HTTP_X_FORWARDED_FOR. If this variable also contains a valid IP address, it is used as the visitor's IP address.
Finally, if neither HTTP_CLIENT_IP nor HTTP_X_FORWARDED_FOR is available or valid, the code resorts to REMOTE_ADDR as the last resort.
By utilizing this comprehensive approach, you can now confidently retrieve visitors' real IP addresses, even when they attempt to conceal them behind proxy servers. This technique empowers web developers with the ability to perform accurate IP-based geolocation, track user activity, and analyze website traffic with greater precision.
The above is the detailed content of How to Get the Real IP Address of Visitors Using PHP, Even When They Are Using Proxies?. For more information, please follow other related articles on the PHP Chinese website!