Get User Location

This is perhaps more a broad PHP question rather than a Kirby specific one but maybe there’s something already built into Kirby that I can leverage.

Is there a way to detect a user location without relying on a 3rd party service like geoplugin.com?
I’m after something that is only granular at the country level, I don’t need anything more accurate than that.

KIrby has the Visitor class, but it only has methods to provide the IP, user agent, but nothing to match the IP to a country.

Yeah I was looking into that and there’s a few things but nothing related to the user location. I see that there’s a preferred language available but that’s not really that safe to use as a substitute.

No, definitely not. The IP is also not reliable, though.

why not use standard js geolocation api and create a small ajax request to backend forwarding an id stored in php session or kirbys user id along with the result of the js geo api?

ah sorry you are looking for a „reverse geocoding“ solution without using google/positionstack.com/geocoder-php.org. sorry dont know any.

A heuristic could be the result of a reverse lookup on the IP, via gethostbyaddr. Then cut off the TLD and many times you’ll have a country code (except for the ISPs that use general “.com” & similar domains)

Also: GitHub - geoacumen/geoacumen-country
But I don’t know how accurate that is; it sources its data from here: https://iptoasn.com/ which probably is easier to parse in PHP

I’m after a php solution, I did consider using geolocation but user can simply refuse to provide the location and that’s not something I can accept unfortunately.

I remember having to setup GeoIP to get Matamo analytics working. Maybe you can tap into that?

What is your use case?

Geo Fencing. Need to display some content only to people from certain countries.

Are you talking about your (registered) users of your site or about any visitor of your site?

For visitors, there is only the option of Geotargeting which basically uses the IP address of the visitor and does a lookup of this address in a database of providers which is publicly available. The provider has a country assigned and thus the country of the provider is assigned to the visitor. Needles to say that this can be bypassed by the visitor to use a different provider, proxy, anonymizor or VPN.

which is called Geoblocking and is considered illegal within the EU with exceptions for copyright use cases. Be careful when implementing this without a good reason.

It’s for a shop. Some products just can’t be shipped to specific countries so I want to avoid displaying a few extra informations. That’s about it.

Just to be clear, it’s not for a marketing reason. They’re just not allowed to sell to those countries.

If the shop is big enough to justify it, you might consider putting it behind CloudFlare. They offer a setting that adds a country code to the request header: Configuring Cloudflare IP Geolocation – Cloudflare Help Center

Just as a reference for future users, I ended up using GeoIP since it was available on the server.

I also want to thank @Adspectus for letting me know about the Geoblocking law which is one of the most bizarre and oddly implement internet laws I have ever come across.

I was going through a bunch of documents and it’s such a confusing implementation of what seems like a good idea. I was reading some FAQs and it clearly states that my use case is allowed

You may refuse to sell certain goods if there is a specific national or EU law which prohibits you to sell certain goods

But in the same document it also says that’s not up to me if the labels on the products are fit for the consumer’s market which is so bizarre and also I’m not liable for selling a product that’s not allowed in the customer’s country.

Such a confusing law.

Sole purpose is to keep lawyers in their jobs. I have yet to see a law that is not confusing.

1 Like