msallehi/php-geolocation
Composer 安装命令:
composer require msallehi/php-geolocation
包简介
A PHP package to restrict access based on user's country/IP location - Works with Laravel, WordPress, and pure PHP
README 文档
README
A PHP package to restrict user access based on their country/IP location. Works with pure PHP, Laravel, WordPress, and any PHP framework.
🇮🇷 مستندات فارسی | 📋 Changelog
✨ Features
- 🌍 Automatic country detection from IP address
- 🔒 Restrict access to specific countries
- ⚙️ Fully customizable configuration
- 🔧 Multiple API provider support
- 📝 Custom error messages
- 🔄 Automatic fallback when API fails (v1.1.0+)
- 🎯 Compatible with pure PHP, Laravel, and WordPress
📦 Installation
composer require msallehi/php-geolocation
🚀 Quick Start
Pure PHP
<?php require 'vendor/autoload.php'; use 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
Static Factory
use MSallehi\GeoLocation\GeoLocation; GeoLocation::create(['allowed_countries' => ['IR']])->guard();
📖 Full Documentation
Basic Usage
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, // ]
Exception Handling
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(); }
Dynamic Configuration
$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();
Check Specific IP
$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');
🔵 Laravel Integration
Register Service Provider
In config/app.php:
'providers' => [ // ... MSallehi\GeoLocation\Laravel\GeoLocationServiceProvider::class, ], 'aliases' => [ // ... 'GeoLocation' => MSallehi\GeoLocation\Laravel\GeoLocationFacade::class, ],
Publish Config
php artisan vendor:publish --tag=geolocation-config
Using Middleware
// 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!'; }); });
Using Facade
use MSallehi\GeoLocation\Laravel\GeoLocationFacade as GeoLocation; if (GeoLocation::isAllowed()) { // User is allowed } $country = GeoLocation::getCountryFromIp();
🟢 WordPress Integration
Basic Setup
In your theme's functions.php or plugin:
require_once get_template_directory() . '/vendor/autoload.php'; require_once get_template_directory() . '/vendor/msallehi/php-geolocation/src/WordPress/functions.php';
Using Helper Functions
// 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();
Using Shortcode
[geo_restrict country="IR"] This content is only visible to Iranian users. [/geo_restrict] [geo_restrict country="IR,US,GB" message="Only for selected countries"] Special content [/geo_restrict]
Restrict Entire Page
// In functions.php add_action('template_redirect', function() { if (is_page('iran-only')) { geo_wp_restrict(['IR']); } });
Custom Restriction in Template
// In template file <?php $geo = new \MSallehi\GeoLocation\GeoLocation([ 'allowed_countries' => ['IR'], 'messages' => ['not_allowed' => 'This page is not available in your country.'] ]); if (!$geo->isAllowed()): ?> <div class="access-denied"> <h2>Access Restricted</h2> <p>Your country: <?php echo $geo->getCountryFromIp(); ?></p> </div> <?php else: ?> <!-- Page content --> <?php endif; ?>
🌐 API Providers
ip-api (Default - Free)
$geo = new GeoLocation([ 'api_provider' => 'ip-api', ]);
ip-api.ir (Iranian API - Recommended for Iranian Servers) 🇮🇷
Optimized for servers located in Iran. Works reliably when international APIs might be slow or blocked.
$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 ]);
📍 Tip: If your server is in Iran, use
ip-api-iras the primary provider andip-apias fallback:$geo = new GeoLocation([ 'api_provider' => 'ip-api-ir', 'fallback_providers' => ['ip-api-ir', 'ip-api'], ]);
ipinfo.io
$geo = new GeoLocation([ 'api_provider' => 'ipinfo', 'ipinfo_token' => 'your-api-token', ]);
ipdata.co
$geo = new GeoLocation([ 'api_provider' => 'ipdata', 'ipdata_api_key' => 'your-api-key', ]);
⚙️ Full Configuration
$config = [ // Allowed countries 'allowed_countries' => ['IR'], // API service 'api_provider' => 'ip-api', // ip-api, ip-api-ir, ipinfo, ipdata // Fallback providers (tried in order if primary fails) 'fallback_providers' => ['ip-api', 'ip-api-ir'], // API keys 'ipinfo_token' => '', 'ipdata_api_key' => '', 'ip_api_ir_guid' => '', // Optional for ip-api.ir // Request timeout 'timeout' => 5, // Local IP 'allow_local' => true, 'local_country' => 'LOCAL', // Error messages 'messages' => [ 'not_allowed' => 'Access from your country is not allowed.', 'api_error' => 'Unable to determine your location.', ], ]; $geo = new GeoLocation($config);
📝 Custom Error Messages
In Constructor
$geo = new GeoLocation([ 'messages' => [ 'not_allowed' => 'Your custom access denied message', 'api_error' => 'Custom API error message', ], ]);
Runtime
$geo->setMessage('not_allowed', 'Access denied from your country.');
With denyAccess
if (!$geo->isAllowed()) { $geo->denyAccess('Your custom message', 403); }
📋 Country Codes
Use ISO 3166-1 alpha-2 codes:
| Country | Code |
|---|---|
| Iran | IR |
| United States | US |
| United Kingdom | GB |
| Germany | DE |
| France | FR |
| Turkey | TR |
| Canada | CA |
| Australia | AU |
| UAE | AE |
| Saudi Arabia | SA |
📄 License
The MIT License (MIT). Please see License File for more information.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
👨💻 Author
- Mohammad Salehi - GitHub
⭐ Support
If you find this package useful, please give it a star on GitHub!
msallehi/php-geolocation 适用场景与选型建议
msallehi/php-geolocation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 483 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「wordpress」 「geolocation」 「laravel」 「IP」 「country」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 msallehi/php-geolocation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 msallehi/php-geolocation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 msallehi/php-geolocation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Maps in minutes. Powered by the Google Maps API.
The official PHP library for apiip.net that allowing customers to automate IP address validation and geolocation lookup in websites, applications and back-office system. Visit our API docs at https://apiip.net/documentation
Supports GeoIP services (sypexgeo.net).
Alfabank REST API integration
Support for multiple GeoIP services.
Support for multiple GeoIP services.
统计信息
- 总下载量: 483
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 38
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-01