I have received many emails about how to do this... it's not really hard and does not need to be scientist... just google for it...
You can either use this: github.com/maxmind/GeoIP2-php [working on PHP 7.x, which I recommend, saves you time of server configuration and it's ready-to-go]
or the old version that works in the majority of the VPS OS [I use debian, it's not working on debian 9]: piwik.org/faq/how-to/faq_164/ [which I do not recommend, because can be deprecated anytime]
-----------------
If you follow up the GeoIP2 guide through Composer setup [available on the first URL I've added here], you will need to add to General Functions the following:
Display All
[Warning: since the GeoIP2 uses Geolite database, it is pretty limited on anonymous proxies or null ip's.]
As you can see it's a very simple snippet.
GeoIsoCode is needed to obtain user ip flag location
GeoName obtains country name
GeoContinent it's useful if you want your users from America pay as USD and Europe pay as EUR.
There are some examples of application:
On Statistics Page:
Display All
And a screenshot of how it is shown
-----------------
By other alternative if you install old geoip tool, you just need to use the following codes:
GeoIsoCode > geoip_country_code_by_name
GeoName > geoip_country_name_by_name
GeoContinent > geoip_continent_code_by_name
More can be seen here > php.net/manual/en/book.geoip.php
You can either use this: github.com/maxmind/GeoIP2-php [working on PHP 7.x, which I recommend, saves you time of server configuration and it's ready-to-go]
or the old version that works in the majority of the VPS OS [I use debian, it's not working on debian 9]: piwik.org/faq/how-to/faq_164/ [which I do not recommend, because can be deprecated anytime]
-----------------
If you follow up the GeoIP2 guide through Composer setup [available on the first URL I've added here], you will need to add to General Functions the following:
PHP Source Code: includes/GeneralFunctions.php
- require 'vendor/autoload.php';
- use GeoIp2\Database\Reader;
- function GeoIsoCode($IP)
- {
- $reader = new Reader('/home/.../public_html/vendor/maxmind-db/GeoLite2-City.mmdb');
- if(isset($IP) && !empty($IP)) {
- $IP = $IP;
- } else {
- $IP = $_SERVER['SERVER_ADDR'];
- }
- $record = $reader->city($IP);
- return $record->country->isoCode;
- }
- function GeoName($IP)
- {
- $reader = new Reader('/home/.../public_html/vendor/maxmind-db/GeoLite2-City.mmdb');
- if(isset($IP) && !empty($IP)) {
- $IP = $IP;
- } else {
- $IP = $_SERVER['SERVER_ADDR'];
- }
- $record = $reader->city($IP);
- return $record->country->name;
- }
- function GeoContinent($IP)
- {
- $reader = new Reader('/home/.../public_html/vendor/maxmind-db/GeoLite2-City.mmdb');
- /* Non formated IP's */
- if(isset($IP) && !empty($IP)) {
- $IP = $IP;
- } else {
- $IP = $_SERVER['SERVER_ADDR'];
- }
- $record = $reader->city($IP);
- return $record->continent->code;
- }
As you can see it's a very simple snippet.
GeoIsoCode is needed to obtain user ip flag location
GeoName obtains country name
GeoContinent it's useful if you want your users from America pay as USD and Europe pay as EUR.
There are some examples of application:
On Statistics Page:
Source Code: includes/pages/game/ShowStatisticsPage.class.php
- foreach ($query as $StatRow)
- {
- $RangeList[] = array(
- 'id' => $StatRow['id'],
- 'name' => $StatRow['username'],
- 'lang' => GeoIsoCode($StatRow['user_lastip']),
- 'langname' => GeoName($StatRow['user_lastip']),
- 'userimage' => $StatRow['userimage'],
- 'points' => pretty_number($StatRow[$Points]),
- 'allyid' => $StatRow['ally_id'],
- 'rank' => $StatRow[$Rank],
- 'allyname' => $StatRow['ally_tag'],
- 'ranking' => $StatRow[$OldRank] - $StatRow[$Rank],
- );
- }
-----------------
By other alternative if you install old geoip tool, you just need to use the following codes:
GeoIsoCode > geoip_country_code_by_name
GeoName > geoip_country_name_by_name
GeoContinent > geoip_continent_code_by_name
More can be seen here > php.net/manual/en/book.geoip.php
The post was edited 5 times, last by Qwa ().