hibit-dev/geodetect
Composer 安装命令:
composer require hibit-dev/geodetect
包简介
Automatically detect user's geo data based on their IP address
README 文档
README
GeoDetect: IP-based country detection
GeoDetect is a framework-agnostic PHP package that allows to effortlessly extract valuable country information from IP addresses. Powered by a robust and up-to-date IP geolocation database, the package provides accurate results and ensures reliable performance. By Integrating GeoDetect into your PHP applications developers can easily implement geolocation functionality without the hassle of building and maintaining their own IP geolocation database.
Installation
Install GeoDetect using composer require:
composer require hibit-dev/geodetect
Usage
Once installed, include the GeoDetect package in your class:
use Hibit\GeoDetect;
Instantiate the class or use the dependency injection:
$geoDetect = new GeoDetect();
Countries
Retrieve country record providing user's IP address:
$country = $geoDetect->getCountry('XXX.XXX.XXX.XXX');
Use available helpers to retrieve the required country information:
$country->getGeonameId(); $country->getIsoCode(); $country->getName(); $country->isInEuropeanUnion();
Also, continent related information:
$country->continent()->getGeonameId(); $country->continent()->getIsoCode();
Self-hosted database
The package utilizes MaxMind's database in the background, which is regularly updated to ensure the accuracy of the data. However, you also have the option to use a self-hosted database by specifying the location of the DB file:
$country = $geoDetect->setCountriesDatabase('location_to_db_file') ->getCountry('XXX.XXX.XXX.XXX');
The package includes MaxMind's database of 2026-07-07
Printing user flag
The GeoDetect package offers the capability to easily display a user's flag. Simply create an image HTML tag and insert the corresponding source for the image based on the country's ISO2 code:
<img alt="Country: FR" src="{{Hibit\GeoDetect::getFlagByIsoCode('FR')}}">
By default, the resulting flag will have a height of 20px and a width that typically varies around 30px.
Sizes can be adjusted and even replaced with an SVG image if needed. To alter the format, the getFlagByIsoCode method requires a second parameter to be provided:
Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::SVG) // SVG format Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::H20) // Height: 20px Width: ~30px Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::H24) // Height: 24px Width: ~36px Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::W20) // Width: 20px Height: ~13px Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::W40) // Width: 40px Height: ~26px
Note: FR was used for illustrative purposes; obtain the country code through the getIsoCode method of the country record. Alternatively, use the 2-character ISO code of the country if it's already available from another source.
Implementation in Laravel
To facilitate testing, we will create a new route within our Laravel application. In the routes/web.php file, a new route called get-country has been added. When this route is accessed, it will return all the available information in JSON format.
Route::get('/get-country', function (Illuminate\Http\Request $request) { $geoDetect = new Hibit\GeoDetect(); $country = $geoDetect->getCountry($request->getClientIp()); return response()->json([ 'continent' => [ 'geonameId' => $country->continent()->getGeonameId(), 'isoCode' => $country->continent()->getIsoCode(), ], 'geonameId' => $country->getGeonameId(), 'isoCode' => $country->getIsoCode(), 'name' => $country->getName(), 'isInEuropeanUnion' => $country->isInEuropeanUnion(), ]); });
Please note that when testing the country detection functionality locally, it may not work as expected. This is because the client IP address in the request instance is typically set to localhost (127.0.0.1). Keep this in mind while testing the country detection feature on your local development environment.
Country names translation
The package comes with configured discovery option to enable Laravel to automatically identify and publish the necessary translations. You can find the translations in the /lang/en/geodetect.php file, and feel free to make any modifications to the translations as needed. If the files are not present, please manually publish them by executing the following command:
php artisan vendor:publish --tag=hibit-geodetect
Once the translations have been published, you can use them to display the country name in any blade file using the translation directive:
@lang('geodetect.ES') //Output: Spain
Alternatively, the following syntax can be used outside the blades:
__('geodetect.ES') //Output: Spain
Documentation
Discover a world of knowledge hosted on HiBit website. Serving as your informational hub, this resource offers clear instructions and valuable insights to explore a spectrum of articles, tutorials, stories, news, and beyond.
You'll find detailed instructions and comprehensive documentation about this repository on:
- GeoDetect: PHP package for IP-based country detection
- Country detection in Laravel applications
- Improved GeoDetect featuring flag recognition
Security
If you discover any security related issues, please email security@hibit.dev instead of using the issue tracker.
About HiBit
HiBit isn't just a blog; it's your go-to space for everything related to development, IT, and the wonders of electronics. Designed for developers, IT enthusiasts, and electronics hobby lovers, HiBit is a dynamic hub that keeps you in the loop with fresh and engaging content.
Explore a collection of articles, tutorials, and insights, encouraging a lively community where reading, commenting, discussing, and sharing experiences is not just promoted but celebrated.
License
The MIT License (MIT). Please see License File for more information.
hibit-dev/geodetect 适用场景与选型建议
hibit-dev/geodetect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.04k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2023 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「geo」 「detection」 「laravel」 「location」 「IP」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hibit-dev/geodetect 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hibit-dev/geodetect 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hibit-dev/geodetect 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shim repository for phpmd/phpmd
Show geo marker on a mapbox map
Show geo shape on a mapbox map with drawing capabilities
The bundle for easy using json-rpc api on your project
Language detection in PSR-15 middleware
统计信息
- 总下载量: 23.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-15
