pendalf89 / yii2-clientinfo
Yii2 客户端信息组件,用于检测真实的客户端IP地址和设备类型
1.0.1
2024-01-23 08:46 UTC
Requires
- mobiledetect/mobiledetectlib: ^4.8
- yiisoft/yii2: ^2.0
README
该组件提供了一种方便的方法来从客户端(用户)获取信息,包括IP地址、国家和设备。它非常快速,这得益于缓存。使用Mobile Detect库来确定设备类型。使用Sypex Geo数据库通过IP地址来确定国家。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
composer require pendalf89/yii2-clientinfo
或者
"pendalf89/yii2-clientinfo": "^1.0.0"
将其添加到您的 composer.json
文件的 require 部分。
配置
'components' => [ 'clientInfo' => 'pendalf89\clientinfo\ClientInfo', ],
您还可以在 Yii.php
文件中添加 DocBlock 以实现 IDE 自动完成。
/** * Class WebApplication * Include only Web application related components here * * @property pendalf89\clientinfo\ClientInfo $clientInfo */ class WebApplication extends yii\web\Application { }
用法
使用非常简单
$isMobile = Yii::$app->clientInfo->isMobile(); $ip = Yii::$app->clientInfo->getIP(); $country = Yii::$app->clientInfo->getCountry(); $isFinland = Yii::$app->clientInfo->isCountry('FI');