digitalpoint
Well-known member
It would be nice if there was 2 new columns in the
So I know not everyone uses Cloudflare, but by default Cloudflare is already sending the country to servers with every request. And you can enable more granular info even for Free plans (like region/city), so it's a terrible easy way to do geotargeting without any APIs or dependencies since the info is already there.
It could be as simple as adding something to the existing
Could of course cram it into a single column, but having the country code as its own allows you to do stuff with it (like show the country flag via device native emojis).
Then if country/region are populated, we can display it (or just show nothing if the record doesn't have the geo info).
xf_ip
table.country
could be 2 digit country code and region
could be a varchar for region.So I know not everyone uses Cloudflare, but by default Cloudflare is already sending the country to servers with every request. And you can enable more granular info even for Free plans (like region/city), so it's a terrible easy way to do geotargeting without any APIs or dependencies since the info is already there.
It could be as simple as adding something to the existing
XF\Entity\Ip->verifyIp()
method... and now we have geotargetted info being stored. And if someone doesn't use Cloudflare or they just want to use some other method to geotarget IPs, they can set the country and region in the entity as they see fit.
PHP:
protected function verifyIp(&$ip)
{
if (!empty($_SERVER['REMOTE_ADDR']) && $ip === $_SERVER['REMOTE_ADDR'])
{
if (!empty($_SERVER['HTTP_CF_IPCOUNTRY']) && strlen($_SERVER['HTTP_CF_IPCOUNTRY']) === 2)
{
$this->country = $_SERVER['HTTP_CF_IPCOUNTRY'];
}
$region = [];
if (!empty($_SERVER['HTTP_CF_IPCITY']))
{
$region[] = $_SERVER['HTTP_CF_IPCITY'];
}
if (!empty($_SERVER['HTTP_CF_REGION']))
{
$region[] = $_SERVER['HTTP_CF_REGION'];
}
if ($region)
{
$this->region = substr(implode(', ', $region), 0, $this->getMaxLength('region'));
}
}
$ip = \XF\Util\Ip::convertIpStringToBinary($ip);
if ($ip === false)
{
// this will fail later
$ip = '';
}
return true;
}
Could of course cram it into a single column, but having the country code as its own allows you to do stuff with it (like show the country flag via device native emojis).
Then if country/region are populated, we can display it (or just show nothing if the record doesn't have the geo info).
Upvote
26