How can I get the user's IP while using both CloudFlare and MaxMind's GeoIP with mod_geoip?

hellodolly picture hellodolly · Jun 24, 2011 · Viewed 11.1k times · Source

CloudFlare provides the user's country from the originating IP but I need location on a city level so I've added MaxMind's GeoCityLite using the mod_geoip Apache script.

The problem is now to get the IP in a php variable, I'm using something like

$country = apache_note("GEOIP_COUNTRY_CODE");

This is great but the IP mod_geoip is using is the CloudFlare DNS, not the end user. CloudFlare offers the server variable HTTP_CF_CONNECTING_IP to use the end-user IP but how do I use THAT variable/IP in the mod_geoip?

Can this be done with a few lines in htaccess?

Edit: I have a workaround using the php API for geoip which is easy but the benchmarks using the apache lookup of the php api is much much better so I'd rather find this solution.

Answer

Eun picture Eun · Jul 19, 2012

do this before you call geoip:

$_SERVER['REMOTE_ADDR'] = isset($_SERVER['HTTP_CF_CONNECTING_IP']) ? $_SERVER['HTTP_CF_CONNECTING_IP'] : $_SERVER['REMOTE_ADDR'];