Download the PHP package lukeyouell/craft-geocookie without Composer
On this page you can find all versions of the php package lukeyouell/craft-geocookie. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lukeyouell/craft-geocookie
More information about lukeyouell/craft-geocookie
Files in lukeyouell/craft-geocookie
Package craft-geocookie
Short Description Collect information about a visitor's location based on their IP address and store the information as a cookie.
License MIT
Informations about the package craft-geocookie
Geo Cookie plugin for Craft CMS 3.x
Collect information about a visitor's location based on their IP address and store the information as a cookie.
Requirements
This plugin requires Craft CMS 3.0.0 or later.
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project
-
Then tell Composer to load the plugin:
composer require lukeyouell/craft-geocookie
- In the Control Panel, go to Settings → Plugins and click the “Install” button for Geo Cookie.
Configuring Geo Cookie
IP Anonymisation
Anonymisation is the process of turning data into a form which does not identify individuals and where identification is not likely to take place. This allows for a much wider use of the information.
You can toggle this on/off in the CP.
API Sources
There are currently 8 different API's to choose from:
- db-ip.com (api key required)
- extreme-ip-lookup.com
- freegeoip.net
- ip-api.com
- ipapi.co (default)
- ipfind.co
- ipinfo.io
- ipvigilante.com
- keycdn.com
API Key
You can supply an API key, should your chosen API source require one.
Request Timeout
You can specify the number of seconds to allow for the request to take place before timing out. The default value is 5 seconds.
Fallback IP Address
This will be used as the fallback ip address, should the plugin fail to source the user's ip address.
Default value is 8.8.8.8
(Google Inc.)
Cookie Name
The default value is geoCookie
although you can change this to whatever you like.
To purge existing cookies simply change the name of the cookie.
Cookie Duration
The user's location is stored in a cookie, you can define the length of time this cookie should exist.
The current options are:
- 1 Hour
- 1 Day
- 1 Week
- 1 Month
- 6 Months
- 1 Year
Using Geo Cookie
Setting the Variable
{% set location = geocookie() %}
The location
variable gives you access to the geolocation data.
Example Usage
JSON Response (ipapi.co)
Usage
Key | Usage | Output |
---|---|---|
ip | {{ location.ip }} | 8.8.8.8 |
city | {{ location.city }} | Mountain View |
region | {{ location.region }} | California |
region_code | {{ location.region_code }} | CA |
country | {{ location.country }} | US |
country_name | {{ location.country_name }} | United States |
postal | {{ location.postal }} | 94035 |
latitude | {{ location.latitude }} | 37.386 |
longitude | {{ location.longitude }} | -122.0838 |
timezone | {{ location.timezone }} | America/Los_Angeles |
asn | {{ location.asn }} | AS15169 |
org | {{ location.org }} | Google Inc. |
Caching
If an active cookie matching the cookie name in the settings exists, the plugin will use the location stored within the cookie instead of performing another API request.
To check if a cookie exists simply use location.cached
which will return a boolean true/false response.
Example API Responses
db-ip.com
extreme-ip-lookup.com
freegeoip.net
ip-api.com
ipapi.co
ipfind.co
ipinfo.io
ipvigilante.com
keycdn.com
Overriding Plugin Settings
If you create a config file in your config
folder called geo-cookie.php
, you can override the plugin’s settings in the Control Panel. Since that config file is fully multi-environment aware, this is a handy way to have different settings across multiple environments.
Here’s what that config file might look like along with a list of all of the possible values you can override.
Parameter | Expected Value |
---|---|
logging |
Boolean |
anonymisation |
Boolean |
apiSource |
extremeiplookup , freegeoip , ipapi , ipapi.com , ipfind , ipinfo , 'ipvigilante', keycdn or db-ip |
apiKey |
API key for chosen source |
requestTimeout |
Integer (seconds) |
fallbackIp |
Valid IP address |
cookieName |
Cookie name |
cookieDuration |
Integer (hours) |
Debugging
You can dump the API response:
Errors are now logged. Simply search your logs for geocookie
.
Geo Cookie Roadmap
Some things to do, and ideas for potential features:
Brought to you by Luke Youell