What are some alternative approaches to detecting a user's country in PHP besides using IP addresses?

Using IP addresses to detect a user's country in PHP can be unreliable due to factors like VPNs and proxies. An alternative approach is to use the Accept-Language header, which browsers send with each request to indicate the user's preferred language. By parsing this header, you can extract the country code and determine the user's location more accurately.

$user_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
preg_match('/\b([A-Z]{2})\b/', $user_language, $matches);
$country_code = $matches[1];

echo "User's country code: " . $country_code;