What is a common method to determine the country of origin of a user in PHP?

One common method to determine the country of origin of a user in PHP is by using an IP geolocation service. These services provide information about the geographic location of an IP address, which can be used to infer the country of the user. One popular service for this purpose is MaxMind's GeoIP database.

// Get the user's IP address
$user_ip = $_SERVER['REMOTE_ADDR'];

// Get the country code using MaxMind's GeoIP database
$country_code = geoip_country_code_by_name($user_ip);

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