kernel / geocookie
根据访客的IP地址收集其位置信息,并将信息存储为cookie。
Requires
- craftcms/cms: ^3.0.0
README
Geo Cookie插件,适用于Craft CMS 3.x
根据访客的IP地址收集其位置信息,并将信息存储为cookie。
需求
此插件需要Craft CMS 3.0.0或更高版本。
安装
要安装插件,请按照以下说明操作。
-
打开您的终端并转到您的Craft项目
cd /path/to/project
-
然后告诉Composer加载插件
composer require lukeyouell/craft-geocookie
-
在控制面板中,转到设置→插件,然后点击Geo Cookie的“安装”按钮。
配置Geo Cookie
IP匿名化
匿名化是将数据转换为不识别个体且不太可能发生识别的形式的过程。这允许更广泛地使用信息。
您可以在CP中切换此功能。
API来源
目前有8种不同的API可供选择
- db-ip.com(需要API密钥)
- extreme-ip-lookup.com
- freegeoip.net
- ip-api.com
- ipapi.co(默认)
- ipfind.co
- ipinfo.io
- ipvigilante.com
- keycdn.com
API密钥
如果您选择的API来源需要,您可以提供API密钥。
请求超时
您可以为请求在超时之前进行的秒数指定秒数。默认值为5秒。
回退IP地址
如果插件无法获取用户的IP地址,则将使用此IP地址作为回退。
默认值为8.8.8.8
(谷歌公司)
Cookie名称
默认值为geoCookie
,尽管您可以将其更改为您喜欢的任何内容。
要清除现有的cookie,只需更改cookie的名称。
Cookie持续时间
用户的地理位置存储在cookie中,您可以定义此cookie应该存在的时长。
当前选项包括
- 1小时
- 1天
- 1周
- 1个月
- 6个月
- 1年
使用Geo Cookie
设置变量
{% set location = geocookie() %}
location
变量使您能够访问地理位置数据。
示例用法
JSON响应(ipapi.co)
{ "ip": "8.8.8.8", "city": "Mountain View", "region": "California", "region_code": "CA", "country": "US", "country_name": "United States", "postal": "94035", "latitude": 37.386, "longitude": -122.0838, "timezone": "America/Los_Angeles", "asn": "AS15169", "org": "Google Inc." }
用法
缓存
如果存在与设置中cookie名称匹配的活动cookie,则插件将使用cookie中存储的位置而不是执行另一个API请求。
要检查是否存在cookie,请使用location.cached
,它将返回布尔true/false响应。
示例API响应
db-ip.com
{ "ipAddress": "8.8.8.8", "continentCode": "NA", "continentName": "North America", "countryCode": "US", "countryName": "United States", "stateProv": "California", "city": "Mountain View" }
extreme-ip-lookup.com
{ "businessName": "", "businessWebsite": "", "city": "Mountain View", "continent": "North America", "country": "United States", "countryCode": "US", "ipName": "google-public-dns-a.google.com", "ipType": "Residential", "isp": "Google", "lat": "37.3860", "lon": "-122.0838", "org": "Google Inc.", "query": "8.8.8.8", "region": "California", "status": "success" }
freegeoip.net
{ "ip": "8.8.8.8", "country_code": "US", "country_name": "United States", "region_code": "", "region_name": "", "city": "", "zip_code": "", "time_zone": "", "latitude": 37.751, "longitude": -97.822, "metro_code": 0 }
ip-api.com
{ "as": "AS15169 Google Inc.", "city": "Mountain View", "country": "United States", "countryCode": "US", "isp": "Google", "lat": 37.4229, "lon": -122.085, "org": "Google", "query": "8.8.8.8", "region": "CA", "regionName": "California", "status": "success", "timezone": "America/Los_Angeles", "zip": "" }
ipapi.co
{ "ip": "8.8.8.8", "city": "Mountain View", "region": "California", "region_code": "CA", "country": "US", "country_name": "United States", "postal": "94035", "latitude": 37.386, "longitude": -122.0838, "timezone": "America/Los_Angeles", "asn": "AS15169", "org": "Google Inc." }
ipfind.co
{ "ip_address": "8.8.8.8", "country": "United States", "country_code": "US", "continent": "North America", "continent_code": "NA", "city": null, "county": null, "region": null, "region_code": null, "timezone": null, "owner": null, "longitude": -97.822, "latitude": 37.751, "currency": "USD", "languages": ["en-US","es-US","haw","fr"] }
ipinfo.io
{ "ip": "8.8.8.8", "hostname": "google-public-dns-a.google.com", "city": "Mountain View", "region": "California", "country": "US", "loc": "37.3860,-122.0840", "org": "AS15169 Google Inc.", "postal": "94035", "phone": "650" }
ipvigilante.com
{ "status": "success", "data": { "ipv4": "8.8.8.8", "continent_name": "North America", "country_name": "United States", "subdivision_1_name": "California", "subdivision_2_name": null, "city_name": "Mountain View", "latitude": "37.38600", "longitude": "-122.08380" } }
keycdn.com
{ "status": "success", "description": "Data successfully received.", "data": { "geo": { "host": "8.8.8.8", "ip": "8.8.8.8", "rdns": "google-public-dns-a.google.com", "asn": "AS15169", "isp": "Google Inc.", "country_name": "United States", "country_code": "US", "region": "", "city": "", "postal_code": "", "continent_code": "NA", "latitude": "37.750999450684", "longitude": "-97.821998596191", "dma_code": "0", "area_code": "0", "timezone": false, "datetime": "na" } } }
覆盖插件设置
如果您在config
文件夹中创建一个名为geo-cookie.php
的配置文件,则可以覆盖控制面板中的插件设置。由于该配置文件完全多环境感知,这是在多个环境中具有不同设置的一种便捷方法。
以下是该配置文件的示例,以及您可以覆盖的所有可能值的列表。
<?php return [ 'logging' => true, 'anonymisation' => true, 'apiSource' => 'keycdn', 'apiKey' => 'lLyl85arR3nT04T4WDt5AVBJrPTBRjUk', 'requestTimeout' => 4, 'fallbackIp' => '8.8.8.8', 'cookieName' => 'geoCookieName', 'cookieDuration' => 168, ];
调试
您可以转储API响应
<pre>{{ location|json_encode(constant('JSON_PRETTY_PRINT')) }}</pre>
错误现在已记录。只需在日志中搜索geocookie
。
Geo Cookie路线图
一些要完成的事情和潜在功能的想法
由Luke Youell提供