// Include GeoIP library require_once('geoip.inc'); // Instantiate GeoIP object $geoip = geoip_open('GeoIP.dat', GEOIP_STANDARD); // Get user's IP $user_ip = $_SERVER['REMOTE_ADDR']; // Get user's country code $user_country = geoip_country_code_by_addr($geoip, $user_ip); // Close GeoIP object geoip_close($geoip); // Save country to session variable $_SESSION['user_country'] = $user_country;
// Include PHP Country List library require_once('php-country-list.php'); // Get user's IP $user_ip = $_SERVER['REMOTE_ADDR']; // Get user's country name $user_country = country_list_get_by_ip($user_ip); // Save country to session variable $_SESSION['user_country'] = $user_country;This code first includes the PHP Country List library and retrieves the user's IP address using the $_SERVER variable. It then uses the country_list_get_by_ip function to get the user's country name based on their IP address. Finally, it saves the country name to a session variable called user_country.