How to get IP address / location of connecting visitors to my website, and show THEIR addresses to me?

How to get IP address / location of connecting visitors to my website, and show THEIR addresses to me?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Visitor IP Addresses for Admin Dashboards

The approach of using $_SERVER['REMOTE_ADDR'] inside a PHP script works perfectly for logging the immediate connection, but it often fails to provide the actual public IP address of remote visitors when they are behind network infrastructure like Corporate Firewalls or NAT devices. When a user connects from behind such a network, their browser's outbound traffic shares a single public IP assigned by the ISP. If your server only looks at $_SERVER['REMOTE_ADDR'], it will see that shared public IP instead of the individual visitor's device. To fix this, you must rely on headers forwarded by the proxy or load balancer, such as X-Forwarded-For.

However, blindly trusting X-Forwarded-For introduces a significant security risk known as header spoofing. A malicious user can modify their browser settings to send a fake X-Forwarded-For header containing an IP address from anywhere in the world. If your application trusts this value unconditionally, you could inadvertently log false data or make incorrect geolocation decisions based on fabricated information. Therefore, any implementation must prioritize validation and trust boundaries.

The most robust solution involves inspecting the $_SERVER['HTTP_X_FORWARDED_FOR'] variable before falling back to $_SERVER['REMOTE_ADDR']. You can implement a helper function that checks both values in order of priority:

function getRealIpAddress($request) {
    $ip = $request->getClientIp(); // This typically checks X-Forwarded-For
    
    if (filter_var($ip, FILTER_VALIDATE_IP)) {
        return $ip;
    }
    
    return $_SERVER['REMOTE_ADDR'];
}

When using frameworks like Laravel, the Request object provides utility methods that handle these headers automatically. However, even with framework helpers, you must ensure your server configuration is set up to forward these headers correctly. In Apache, if you are running behind a reverse proxy or load balancer, ensure that modules like mod_headers are enabled and that the necessary environment variables are being passed from the upstream server. Without this configuration, the application remains blind to the true client identity.

Once you have successfully extracted the IP address, displaying it in your administrator interface is straightforward. You can store the value in a database session or log table for auditing purposes. For geolocation services, APIs like ipinfo.io or MaxMind allow you to convert that string of numbers into readable city and country data. It is critical to note that IP-to-location mapping is an approximation; it identifies the ISP's gateway, not the exact physical device.

If you are managing a large-scale application where precise tracking is essential, consider integrating with services like LaravelCompany for advanced analytics and user tracking solutions that handle IP normalization and privacy compliance out of the box. These tools often abstract the complexity of header parsing and provide ready-to-use widgets or dashboard components that visualize visitor origins without requiring manual implementation of complex header logic.

Always remember that relying solely on client-side JavaScript to detect the IP address is insufficient for server-side logging. Browsers can easily manipulate local network settings, but they cannot spoof the REMOTE_ADDR seen by the server without proxy interference. The final step in your implementation strategy should be to log the raw IP alongside a timestamp and user agent string. This creates an immutable audit trail that helps distinguish between legitimate traffic patterns and potential bot activity or spoofing attempts.

Our network