How can PHP be used to retrieve the client's IP address and differentiate between external and local access?

To retrieve the client's IP address in PHP, you can use the $_SERVER['REMOTE_ADDR'] variable. To differentiate between external and local access, you can compare the client's IP address with known local IP ranges (e.g., 127.0.0.1 for localhost). This can be useful for implementing security measures or custom functionality based on the type of access.

$client_ip = $_SERVER['REMOTE_ADDR'];

// Check if the client's IP address is within local IP ranges
$local_ip_ranges = array('127.0.0.1', '::1'); // Add more local IP ranges if needed
$is_local_access = in_array($client_ip, $local_ip_ranges);

if($is_local_access){
    echo "Local access from IP: " . $client_ip;
} else {
    echo "External access from IP: " . $client_ip;
}