1. Go to this page and download the library: Download msallehi/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/ */
msallehi / php-geolocation example snippets
MSallehi\GeoLocation\GeoLocation;
// Create instance with default settings (Iran only)
$geo = new GeoLocation();
// Or with custom settings
$geo = new GeoLocation([
'allowed_countries' => ['IR', 'TR'],
'messages' => [
'not_allowed' => 'Access from your country is not allowed.',
],
]);
// Check access
if ($geo->isAllowed()) {
echo "Welcome!";
} else {
echo "Access denied";
}
// Or use guard for automatic blocking
$geo->guard(); // Automatically returns 403 error if not allowed
use MSallehi\GeoLocation\GeoLocation;
GeoLocation::create(['allowed_countries' => ['IR']])->guard();
use MSallehi\GeoLocation\GeoLocation;
$geo = new GeoLocation();
// Get user's country
$country = $geo->getCountryFromIp();
echo "Your country: " . $country; // IR, US, GB, ...
// Get user's IP
$ip = $geo->getClientIp();
// Check if allowed
if ($geo->isAllowed()) {
// User is allowed
}
// Get full location details
$location = $geo->getLocationDetails();
// [
// 'ip' => '5.160.139.15',
// 'country_code' => 'IR',
// 'country_name' => 'Iran',
// 'city' => 'Tehran',
// 'region' => 'Tehran',
// 'is_local' => false,
// ]
use MSallehi\GeoLocation\GeoLocation;
use MSallehi\GeoLocation\Exceptions\CountryNotAllowedException;
use MSallehi\GeoLocation\Exceptions\GeoLocationException;
$geo = new GeoLocation(['allowed_countries' => ['IR']]);
try {
$geo->validate();
// User is allowed
} catch (CountryNotAllowedException $e) {
echo $e->getMessage();
echo "Your country: " . $e->getDetectedCountry();
echo "Allowed countries: " . implode(', ', $e->getAllowedCountries());
// Convert to JSON
echo $e->toJson();
} catch (GeoLocationException $e) {
echo "Location detection error: " . $e->getMessage();
}
$geo = new GeoLocation();
// Change allowed countries
$geo->setAllowedCountries(['IR', 'US', 'GB']);
// Add country
$geo->addAllowedCountry('DE');
// Remove country
$geo->removeAllowedCountry('US');
// Change error message
$geo->setMessage('not_allowed', 'This service is not available in your country.');
// Change API Provider
$geo->setApiProvider('ipinfo', ['token' => 'your-token']);
// Check specific country
if ($geo->isCountryAllowed('IR')) {
echo "Iran is allowed";
}
// Get allowed countries list
$countries = $geo->getAllowedCountries();
$geo = new GeoLocation(['allowed_countries' => ['IR']]);
// Check specific IP
$isAllowed = $geo->isAllowed('5.160.139.15');
$country = $geo->getCountryFromIp('8.8.8.8');
$location = $geo->getLocationDetails('1.1.1.1');
// routes/web.php
// Only Iranian users
Route::middleware(['geolocation'])->group(function () {
Route::get('/iran-only', function () {
return 'Welcome!';
});
});
// Specify countries in middleware
Route::middleware(['geolocation:IR,US,GB'])->group(function () {
Route::get('/multi-country', function () {
return 'Welcome!';
});
});
use MSallehi\GeoLocation\Laravel\GeoLocationFacade as GeoLocation;
if (GeoLocation::isAllowed()) {
// User is allowed
}
$country = GeoLocation::getCountryFromIp();
// Get country
$country = geo_get_country();
// Check access
if (geo_is_allowed()) {
echo "Welcome!";
}
// Automatic block
geo_guard(); // Calls wp_die if not allowed
// Set countries
geo_set_countries(['IR', 'TR']);
// Get full details
$location = geo_get_location();
// In functions.php
add_action('template_redirect', function() {
if (is_page('iran-only')) {
geo_wp_restrict(['IR']);
}
});
// In template file
$geo = new \MSallehi\GeoLocation\GeoLocation([
'allowed_countries' => ['IR'],
'messages' => ['not_allowed' => 'This page is not available in your country.']
]);
if (!$geo->isAllowed()):
$geo = new GeoLocation([
'api_provider' => 'ip-api',
]);
$geo = new GeoLocation([
'api_provider' => 'ip-api-ir',
]);
// With optional GUID for premium features
$geo = new GeoLocation([
'api_provider' => 'ip-api-ir',
'ip_api_ir_guid' => 'your-guid', // Optional
]);