Support

Admin Tools

#25383 GeoIP Usage Examples and Possible Cron for Auto-Updating Database?

Posted in ‘Admin Tools for Joomla! 4 & 5’
This is a public ticket

Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.

Environment Information

Joomla! version
n/a
PHP version
n/a
Admin Tools version
n/a

Latest post by on Friday, 15 July 2016 17:20 CDT

magestyx
I've scoured the support forums here on the GeoIP plugin but couldn't find what I needed.
I've used the Maxmind geoIP before via package installations on the server, but I'd like to use yours now that it's integrated into Joomla as a plugin. I was about to ask how to use the geoip functions wherever I want when I got it working, so I thought I'd share. But if anyone has a better or more technically sound way please post it.

I added the below to my template and it works fine for me:

<?php

if (!class_exists('AkeebaGeoipProvider')){
if (@file_exists(JPATH_PLUGINS . '/system/akgeoip/lib/akgeoip.php')){
if (@include_once JPATH_PLUGINS . '/system/akgeoip/lib/vendor/autoload.php')
{
@include_once JPATH_PLUGINS . '/system/akgeoip/lib/akgeoip.php';
}
}
}

$geoip = new AkeebaGeoipProvider();

$ip = $_SERVER["REMOTE_ADDR"];
$countryCode2 = $geoip->getCountryCode($ip);
echo 'ip ' . $ip . ' is located in: ' . $countryCode2;

?>

Now, correct me if I'm wrong, but any of the public functions in /plugins/system/akgeoip/lib/akgeoip.php can be called, right? So with very little modification couldn't a standalone PHP file be targeted with CRON to call the updateDatabase() function and auto-update the geoIP database regularly?

Thanks,
Kalin

nicholas
Akeeba Staff
Manager
Now, correct me if I'm wrong, but any of the public functions in /plugins/system/akgeoip/lib/akgeoip.php can be called, right?


Correct. All public functions are part of the public API which is meant to be called by other software.

couldn't a standalone PHP file be targeted with CRON to call the updateDatabase() function and auto-update the geoIP database regularly?


No, not a standalone file. Since that class assumes that it lives inside Joomla (since it's part of a Joomla plugin) you'd need to run it from a Joomla CLI script. As you can see the updateDatabase() method calls JText, JFile and other Joomla! API files.

That said, you don't need to go through that to update the database. You can use a fairly simple CRON command line to download and extract the database file:

cd /path/to/site/plugins/system/akgeoip/db; wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz -O GeoLite2-Country.mmdb.gz; gunzip GeoLite2-Country.mmdb.gz


IMPORTANT! Replace /path/to/site/ with the absolute filesystem path to your site, e.g. /home/myuser/public_html/ (ask your host).

MaxMind updates the database once a month. There's no much point running your CRON job more frequently than twice a month.

Nicholas K. Dionysopoulos

Lead Developer and Director

🇬🇷Greek: native 🇬🇧English: excellent 🇫🇷French: basic • 🕐 My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

magestyx
Ah - of course - that makes sense it needs the Joomla API.

The cron script is a huge help - thanks much!

Kalin

nicholas
Akeeba Staff
Manager
You're welcome!

Nicholas K. Dionysopoulos

Lead Developer and Director

🇬🇷Greek: native 🇬🇧English: excellent 🇫🇷French: basic • 🕐 My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

System Task
system
This ticket has been automatically closed. All tickets which have been inactive for a long time are automatically closed. If you believe that this ticket was closed in error, please contact us.

Support Information

Working hours: We are open Monday to Friday, 9am to 7pm Cyprus timezone (EET / EEST). Support is provided by the same developers writing the software, all of which live in Europe. You can still file tickets outside of our working hours, but we cannot respond to them until we're back at the office.

Support policy: We would like to kindly inform you that when using our support you have already agreed to the Support Policy which is part of our Terms of Service. Thank you for your understanding and for helping us help you!