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.
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.
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)
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.
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.
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.