[snip] User location flag

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • [snip] User location flag

      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:


      PHP Source Code: includes/GeneralFunctions.php

      1. require 'vendor/autoload.php';
      2. use GeoIp2\Database\Reader;
      3. function GeoIsoCode($IP)
      4. {
      5. $reader = new Reader('/home/.../public_html/vendor/maxmind-db/GeoLite2-City.mmdb');
      6. if(isset($IP) && !empty($IP)) {
      7. $IP = $IP;
      8. } else {
      9. $IP = $_SERVER['SERVER_ADDR'];
      10. }
      11. $record = $reader->city($IP);
      12. return $record->country->isoCode;
      13. }
      14. function GeoName($IP)
      15. {
      16. $reader = new Reader('/home/.../public_html/vendor/maxmind-db/GeoLite2-City.mmdb');
      17. if(isset($IP) && !empty($IP)) {
      18. $IP = $IP;
      19. } else {
      20. $IP = $_SERVER['SERVER_ADDR'];
      21. }
      22. $record = $reader->city($IP);
      23. return $record->country->name;
      24. }
      25. function GeoContinent($IP)
      26. {
      27. $reader = new Reader('/home/.../public_html/vendor/maxmind-db/GeoLite2-City.mmdb');
      28. /* Non formated IP's */
      29. if(isset($IP) && !empty($IP)) {
      30. $IP = $IP;
      31. } else {
      32. $IP = $_SERVER['SERVER_ADDR'];
      33. }
      34. $record = $reader->city($IP);
      35. return $record->continent->code;
      36. }
      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:

      Source Code: includes/pages/game/ShowStatisticsPage.class.php

      1. foreach ($query as $StatRow)
      2. {
      3. $RangeList[] = array(
      4. 'id' => $StatRow['id'],
      5. 'name' => $StatRow['username'],
      6. 'lang' => GeoIsoCode($StatRow['user_lastip']),
      7. 'langname' => GeoName($StatRow['user_lastip']),
      8. 'userimage' => $StatRow['userimage'],
      9. 'points' => pretty_number($StatRow[$Points]),
      10. 'allyid' => $StatRow['ally_id'],
      11. 'rank' => $StatRow[$Rank],
      12. 'allyname' => $StatRow['ally_tag'],
      13. 'ranking' => $StatRow[$OldRank] - $StatRow[$Rank],
      14. );
      15. }
      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
      Looking for good VPS to host your game? Use DigitalOcean and get 10$ bonus credit!

      Stellaron - Grand Strategy OGame Universe, discover the secrets of Ancient Technology
      Upcoming expansion: Stellaron StarGate

      The post was edited 5 times, last by Qwa ().