Support

Admin Tools

#9904 PHP Fatal error: Cannot redeclare geoip_country_code_by_name

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 nicholas on Wednesday, 14 September 2011 03:57 CDT

user23381
Mandatory information about my setup:

Have I searched the forum before posting? Yes
Have I read the Troubleshooting Wizard before posting? Yes
Have I read the documentation before posting? Yes
Joomla! version: 1.5.23
PHP version: 5
MySQL version: 5
Admin Tools Professional version: 2.0.5


Description of my issue:

I installed the PHP PECL extension GeoIP (http://php.net/manual/en/book.geoip.php) and that caused the following error:

PHP Fatal error: Cannot redeclare geoip_country_code_by_name() in /administrator/components/com_admintools/helpers/geoip.php on line 376

Once I fixed that issue, the next function also caused a problem so all I had to do was wrap the functions in a function_exists check, like this:

if (!function_exists('geoip_country_code_by_name'))
{
function geoip_country_code_by_name($gi, $name) {
$country_id = geoip_country_id_by_name($gi,$name);
if ($country_id !== false) {
return $gi->GEOIP_COUNTRY_CODES[$country_id];
}
return false;
}
}

if (!function_exists('geoip_country_name_by_name'))
{
function geoip_country_name_by_name($gi, $name) {
$country_id = geoip_country_id_by_name($gi,$name);
if ($country_id !== false) {
return $gi->GEOIP_COUNTRY_NAMES[$country_id];
}
return false;
}
}

That fixed the Fatal Error issue.

slaes
are you using a commercial license for maxmind, by any chance. I've been meaning to play with it at some point, but from what i have read is most effective in the US.

nicholas
Akeeba Staff
Manager
Nice catch! There is a more elegant solution. Instead of wrapping the geoip.php include file in a function_exists wrapper, we can simply wrap the require_once of that file in plugins/system/admintools/pro.php ;) I am going to add that fix to version 2.1. Thank you for the heads up!

Regarding MaxMind, no, I do not use their professional license. Doing so would require obtaining a license per installation of Admin Tools, which would cost Admin Tools to cost several hundred dollars. Instead, I am using their free database. There are instructions in the documentation for upgrading it to the latest release, or using one of their licensed versions.

According to my experience, their database is very accurate, at least to country level (that's what we're using anyway). When you're talking about determining city, state, ZIP code or even address... well, that's next to impossible. In the typical case, your ISP's switch is not even located in the city you live in. I have two houses, 50 Km apart. Their addresses, based on my IP address, are downtown Athens which is about 12 and 48 Km off in each case :D So, I wouldn't trust IP databases for anything other than country-level geolocation.

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!

user23381
@slaes - Yes the free GeoCityLite DB put a California, USA IP in England so I purchased the region hoping for a more accurate result. Unfortunately it also put the IP in England :( I'm sure it's more accurate but it's still got it's problems. One thing I would recommend is if you're doing a lot of lookups, install the PECL module for GeoIP lookups. According to the Maxmind benchmarks it is much, much faster then the plain PHP API.

@nicholas - That sounds like a good solution. Thanks.

slaes
yeah i thought as much. will play with it either way and see what results i can get.

personally im not a fan of ip block, country specific anyway. however i can see some really good uses relative to adds and a few other things.

slaes
and thanks for your update boys! :)

nicholas
Akeeba Staff
Manager
The IP-to-Country does have some limitations. Even MaxMind claims a 99% accuracy. This is not a big percentage. On a site with 100,000 uniques per month, it means that 1,000 uniques will be flagged as coming from the wrong country. Bummer!

I am not a fan of IP blocking either. I consider the country of origin to be nothing but a useful auxiliary piece of information when you are going through the security exceptions log file and that's about it. Some people were begging me for IP and country blocking, that's why I added those features. Well, they can use these features at their own risk :)

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!

user44231
Sorry for resurrecting this thread, but I'm currently running into a similiar issue with a Joomla setup.

I'm using AdminTools version 2.1.6, the PHP version currently running on the webserver is 5.2.17, Joomla version is 1.5.23.

Using the "function_exists" wrapping in geoip.php as described in the first post gets rid of the "fatal" errors, but results in several additional notices:

Notice: Constant GEOIP_ASNUM_EDITION already defined in /.../com_admintools/helpers/geoip.php on line 33
Notice: Constant GEOIP_NETSPEED_EDITION already defined in /.../com_admintools/helpers/geoip.php on line 34
Notice: Constant GEOIP_REGION_EDITION_REV0 already defined in /.../com_admintools/helpers/geoip.php on line 35
Notice: Constant GEOIP_REGION_EDITION_REV1 already defined in /.../com_admintools/helpers/geoip.php on line 36
Notice: Constant GEOIP_CITY_EDITION_REV0 already defined in /.../com_admintools/helpers/geoip.php on line 37
Notice: Constant GEOIP_CITY_EDITION_REV1 already defined in /.../com_admintools/helpers/geoip.php on line 38
Notice: Constant GEOIP_ORG_EDITION already defined in /.../com_admintools/helpers/geoip.php on line 39
Notice: Constant GEOIP_ISP_EDITION already defined in /.../com_admintools/helpers/geoip.php on line 40
Notice: Constant GEOIP_UNKNOWN_SPEED already defined in /.../com_admintools/helpers/geoip.php on line 51
Notice: Constant GEOIP_DIALUP_SPEED already defined in /.../com_admintools/helpers/geoip.php on line 52
Notice: Constant GEOIP_CABLEDSL_SPEED already defined in /.../com_admintools/helpers/geoip.php on line 53
Notice: Constant GEOIP_CORPORATE_SPEED already defined in /.../com_admintools/helpers/geoip.php on line 54

I'm not really sure if there's anything server-side (e.g. some other PHP module) that's causing these conflicts (this is a shared hosting setup).

Is there any chance of enhancing the handling of the geoip code in a future version of AdminTools to provide a better workaround for these issues?

Thanks in advance for your help!

nicholas
Akeeba Staff
Manager
Admin Tools 2.1.6, plugins/system/admintools/pro.php, line 1022:1024:

if(!function_exists('geoip_open')) {
				require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_admintools'.DS.'helpers'.DS.'geoip.php';
			}


Since you say that you don't have this, you either haven't got Admin Tools 2.1.6 or your update failed.

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!

nicholas
Akeeba Staff
Manager
Ah, spoke too soon. I can see that there are another two places where this thing loads. Even though I had fixed the OP's issue, I hadn't replaced all instances, so I guess you're right :)

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!

user44231
I made sure I had the corresponding line in plugins/system/admintools/pro.php before posting. ;)

Strange thing is, I also spotted the other two places where geoip.php is referenced in the file, also added the 'function_exists' fencing there, but still got the error.

I suspect that whatever other PHP module is loaded on this server it seems to export several of the geoip function names and variables but probably not 'geoip_open', resulting in geoip.php still being loaded.

Adding checks to each function and variable used in geoip.php and only registering those that are not already present would probably fix the issues, but this seems like a very unelegant solution...

nicholas
Akeeba Staff
Manager
This would be the optional PHP GeoIP extension. And, yes, it doesn't export the geoip_open() function, but it does export geoip_country_code_by_name().

I just fixed that in the code and uploading a new dev release (svn 391). Can you please check it and tell me if it works for you?

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!

user44231
Hmm... running this code:

<?php 
// ALL USER DEFINED FUNCTIONS 
$arr = get_defined_functions(); 
foreach ($arr[&#039;user&#039;] as $key => $value){ 
echo $value.' (is user-defined) '; 
} 
// ALL USER DEFINED FUNCTIONS 

// ALL INTERNAL FUNCTIONS 
$arr = get_defined_functions(); 
foreach ($arr[&#039;internal&#039;] as $key => $value){ 
echo $value.' (is internal) '; 
} 
// ALL INTERNAL FUNCTIONS 
?>


on the webserver results in the following output:

......

imagefilter (is internal)

imageconvolution (is internal)

geoip_database_info (is internal)

geoip_country_code_by_name (is internal)

geoip_country_code3_by_name (is internal)

geoip_country_name_by_name (is internal)

geoip_continent_code_by_name (is internal)

geoip_org_by_name (is internal)

geoip_record_by_name (is internal)

geoip_id_by_name (is internal)

geoip_region_by_name (is internal)

geoip_isp_by_name (is internal)

geoip_db_avail (is internal)

geoip_db_get_all_info (is internal)

geoip_db_filename (is internal)

geoip_region_name_by_code (is internal)

geoip_time_zone_by_country_and_region (is internal)

gmp_init (is internal)

gmp_intval (is internal)

gmp_strval (is internal)

gmp_add (is internal)

......


Which seems to support my suspicion from the previous post (sorry if I'm on the wong track here, I'm not really knowledgeable in PHP). ;)

user44231
I just fixed that in the code and uploading a new dev release (svn 391). Can you please check it and tell me if it works for you?


Thanks, I'll test this right away!

nicholas
Akeeba Staff
Manager
Your suspicion is correct. Look my post above (I think you posted exactly the same time as me :D)

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!

user44231
Nope, the SVN version still produces the fatal error.

Fatal error: Cannot redeclare geoip_country_code_by_name() in /.../com_admintools/helpers/geoip.php on line 376

nicholas
Akeeba Staff
Manager
Yeah, I made a typo. Try the new one, svn 392 (it may take a few minutes to see it on-line). This one should work.

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!

user44231
The pro.php from com_admintools-svn391-pro.zip still references the geoip_open function btw:

if(!function_exists('geoip_open')) {
require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_admintools'.DS.'helpers'.DS.'geoip.php';[/code]

Not sure if you changed something else or just put in the wrong file. ;)

user44231
Revision 392 works!

Only two notices left:

Notice: geoip_country_code_by_name() [function.geoip-country-code-by-name]: Host x.x.x.x not found in /.../plugins/system/admintools/pro.php on line 1282
Notice: geoip_continent_code_by_name() [function.geoip-continent-code-by-name]: Host x.x.x.x not found in /.../plugins/system/admintools/pro.php on line 1283


This host is in the IP whitelist, maybe I need to redefine it. Will check that right away.

user44231
Nope, the two notices are still there. It's the source IP I'm using when accessing the website.

user44231
http://us2.php.net/manual/en/function.geoip-country-code-by-name.php seems to suggest this is normal if the GeoIP function cannot determine the origin of the IP address.

So I guess this works as designed now?

nicholas
Akeeba Staff
Manager
Yes, it is working properly now. You just have to ask your host to either disable the geoip extension or install/update the system-wide GeoIP database. Unfortunately, the geoip extension does not allow loading arbitrary GeoIP databases from disk. They have to be placed in a central location by someone who has root access on the server :(

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!

user44231
Okay, I'll be bugging the hosting company about that. ;)

Thanks for your help!

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!

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!