1. Go to this page and download the library: Download rumenx/php-geolocation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rumenx / php-geolocation example snippets
use Rumenx\Geolocation\Geolocation;
// Create a simulated instance for a specific country
$geo = Geolocation::simulate('DE', [
'DE' => ['de'],
'CA' => ['en', 'fr']
]);
echo $geo->getCountryCode(); // 'DE'
echo $geo->getIp(); // Simulated IP like '192.168.4.123'
use Rumenx\Geolocation\GeolocationSimulator;
// Generate fake Cloudflare headers
$headers = GeolocationSimulator::fakeCloudflareHeaders('JP', [
'user_agent' => 'Custom User Agent',
'server_name' => 'dev.example.com'
]);
// Create instance with simulated server data
$geo = new Geolocation($headers, ['JP' => ['ja', 'en']]);
$geo = new Geolocation();
if ($geo->isLocalDevelopment()) {
// Automatically detected: localhost, local IPs, or missing Cloudflare headers
echo "Running in local development mode";
}
// Get list of built-in countries
$countries = GeolocationSimulator::getAvailableCountries();
// ['US', 'CA', 'GB', 'DE', 'FR', 'JP', 'AU', 'BR']
// Get random country for testing
$randomCountry = GeolocationSimulator::randomCountry();
use Rumenx\Geolocation\Geolocation;
// Simple usage with defaults
$geo = new Geolocation();
$country = $geo->getCountryCode();
$ip = $geo->getIp();
$info = $geo->getGeoInfo();
// Advanced usage with custom configuration
$countryToLanguage = [
'CA' => ['en', 'fr'], // Canada: English (default), French
'DE' => ['de'], // Germany: German
'CH' => ['de', 'fr', 'it'], // Switzerland: German, French, Italian
// Add more countries as needed...
];
$geo = new Geolocation(
$_SERVER, // HTTP server array (optional, defaults to $_SERVER)
$countryToLanguage, // Country-to-language mapping (optional)
'my_lang_cookie' // Custom cookie name (optional, defaults to 'lang')
);
// Get best language for visitor based on country and browser preferences
$availableSiteLanguages = ['en', 'fr', 'de', 'es'];
$lang = $geo->getLanguageForCountry(null, $availableSiteLanguages);
// Language selection logic:
// 1. If browser preferred language matches a country language and is available, use it
// 2. Else, check all browser languages for a match with available languages
// 3. Else, use the first country language as fallback
// 4. Returns null if no match found
// Check if language should be set (based on cookie)
if ($geo->shouldSetLanguage()) {
// Set language in your application
setcookie($geo->languageCookieName, $lang);
}
// Get specific information
$country = $geo->getCountryCode(); // 'US', 'CA', 'DE', etc.
$ip = $geo->getIp(); // '192.168.1.1'
$browser = $geo->getBrowser(); // ['name' => 'Chrome', 'version' => '91.0']
$os = $geo->getOs(); // 'Windows 10', 'macOS', 'Linux', etc.
$device = $geo->getDeviceType(); // 'desktop', 'mobile', 'tablet'
$resolution = $geo->getResolution(); // ['width' => 1920, 'height' => 1080]
// Get all information at once
$info = $geo->getGeoInfo();
// Returns: [
// 'country_code' => 'US',
// 'ip' => '192.168.1.1',
// 'preferred_language' => 'en-US',
// 'all_languages' => ['en-US', 'en', 'fr'],
// 'user_agent' => 'Mozilla/5.0...',
// 'browser' => ['name' => 'Chrome', 'version' => '91.0'],
// 'os' => 'Windows 10',
// 'device_type' => 'desktop',
// 'resolution' => ['width' => 1920, 'height' => 1080]
// ]
// Get only specific fields
$specificInfo = $geo->getGeoInfo(['country_code', 'ip', 'browser']);
// In a controller
class HomeController extends Controller
{
public function index(Request $request)
{
$geo = new Geolocation(
$request->server->all(),
config('app.country_to_language', [])
);
$country = $geo->getCountryCode();
$lang = $geo->getLanguageForCountry(null, ['en', 'fr', 'de']);
if ($geo->shouldSetLanguage() && $lang) {
app()->setLocale($lang);
}
return view('home', [
'geo' => $geo->getGeoInfo(['country_code', 'ip']),
'language' => $lang
]);
}
}
// In a middleware (optional)
class GeolocationMiddleware
{
public function handle($request, Closure $next)
{
$geo = new Geolocation($request->server->all());
$lang = $geo->getLanguageForCountry();
if ($lang && $geo->shouldSetLanguage()) {
app()->setLocale($lang);
}
return $next($request);
}
}
// In a controller
class HomeController extends AbstractController
{
public function index(Request $request): Response
{
$geo = new Geolocation(
$request->server->all(),
$this->getParameter('country_to_language')
);
$country = $geo->getCountryCode();
$lang = $geo->getLanguageForCountry(null, ['en', 'fr', 'de']);
if ($geo->shouldSetLanguage() && $lang) {
$request->setLocale($lang);
}
return $this->render('home.html.twig', [
'geo' => $geo->getGeoInfo(),
'language' => $lang
]);
}
}
// In an event listener (optional)
class GeolocationListener
{
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$geo = new Geolocation($request->server->all());
$lang = $geo->getLanguageForCountry();
if ($lang && $geo->shouldSetLanguage()) {
$request->setLocale($lang);
}
}
}
// In any PHP application
session_start();
$geo = new Geolocation($_SERVER, [
'US' => ['en'],
'CA' => ['en', 'fr'],
'DE' => ['de'],
'FR' => ['fr']
]);
$country = $geo->getCountryCode();
$lang = $geo->getLanguageForCountry(null, ['en', 'fr', 'de']);
// Set language preference
if ($geo->shouldSetLanguage() && $lang) {
$_SESSION['language'] = $lang;
setcookie('lang', $lang, time() + (86400 * 30)); // 30 days
}
// Use the information
echo "Welcome visitor from: " . ($country ?? 'Unknown');
echo "Preferred language: " . ($lang ?? 'Default');
$geo = new Geolocation($server, $countryToLanguage, $languageCookieName);
$countryToLanguage = [
'US' => ['en'], // United States: English only
'CA' => ['en', 'fr'], // Canada: English (default), French
'CH' => ['de', 'fr', 'it', 'rm'], // Switzerland: German (default), French, Italian, Romansh
'BE' => ['nl', 'fr', 'de'], // Belgium: Dutch (default), French, German
'IN' => ['hi', 'en'], // India: Hindi (default), English
'ZA' => ['en', 'af', 'zu'], // South Africa: English (default), Afrikaans, Zulu
// Add more countries as needed...
];
// Use defaults for everything
$geo = new Geolocation();