Best way to identify the user's country of origin?

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Best way to identify the user's country of origin?

Post by Joubarbe »

Hi,

I have a book on Amazon that I want to advertise through my game, but what I'm currently doing is to open amazon.com to anyone who is clicking the link, which is not great, as the book appears to be out of stock if you're not in your Amazon region. So the best would be to have a condition there that says "if you're coming from the UK, open amazon.co.uk, if you're coming from France, open amazon.fr, etc.".

How to do that?
User avatar
Caronte3D
Addict
Addict
Posts: 1362
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Best way to identify the user's country of origin?

Post by Caronte3D »

Try something like...

Code: Select all

Define Buffer$ = "", buflen, bytesread, LC
LC             = #LOCALE_SLANGUAGE
buflen         = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, 0)
Buffer$        = Space(buflen)
bytesread      = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, buflen)

Debug "Result=" + Buffer$
User avatar
NicTheQuick
Addict
Addict
Posts: 1523
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Best way to identify the user's country of origin?

Post by NicTheQuick »

There are third party redirection services that can also do that if that is okay for you. Here's just one I found: https://geniuslink.com/
I am sure there are more out there.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Best way to identify the user's country of origin?

Post by Joubarbe »

Thanks Caronte!

Uhh... Would you mind explaining this code actually? Especially this first line, considering that the following also works: (are you just declaring those variables in a weird way? :D )

Code: Select all

Define Buffer$ = "";, buflen, bytesread, LC

LC             = #LOCALE_SLANGUAGE
buflen         = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, 0)
Buffer$        = Space(buflen)
bytesread      = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, buflen)

Debug "Result=" + Buffer$
Thank you NicTheQuick, I didn't know about that.
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Best way to identify the user's country of origin?

Post by Joubarbe »

It would actually be good to obtain the country code of the user. Instead of a string.
User avatar
Caronte3D
Addict
Addict
Posts: 1362
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Best way to identify the user's country of origin?

Post by Caronte3D »

Joubarbe wrote: Thu Apr 10, 2025 10:48 am Uhh... Would you mind explaining this code actually? Especially this first line...
Ever is good practice to declare variables (and use EnableExplicit at first line of your codes) to get rid of bugs.
User avatar
Caronte3D
Addict
Addict
Posts: 1362
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Best way to identify the user's country of origin?

Post by Caronte3D »

Joubarbe wrote: Thu Apr 10, 2025 11:25 am It would actually be good to obtain the country code of the user. Instead of a string.
Country code:

Code: Select all

EnableExplicit

Define Buffer$ = "", buflen, bytesread, countryCode, LC
LC             = #LOCALE_ICOUNTRY
buflen         = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, 0)
Buffer$        = Space(buflen)
bytesread      = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, buflen)
countryCode    = Val(Buffer$)
Debug "Country (code)=" + Str(countryCode)
P.D: You can omit the "bytesread" line, I use it in case it returns 0 bytes and use a default language.
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Best way to identify the user's country of origin?

Post by Joubarbe »

Thanks!
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Best way to identify the user's country of origin?

Post by blueb »

Well, I use a VPN, and I change countries at will, depending on the connection speed, etc.
The code above doesn't work, for me it always displays: Country (code)=1 even though I'm in Mexico

I fired up Okla Speed test, changed my VPN to Argentina, ran a speed test to be sure I was actually in Argentina.
It confirmed my location, but my results never changed.
So you might have to use a different logic, to arrive at the users location.

I was able to find this code to get a users actual location, you'll need to parse the JSON text to find the location.

Code: Select all

EnableExplicit

Define URL.s, Response.s
Define HTTPRequest.i

URL = "http://ip-api.com/json/" ; A free geolocation service

HTTPRequest = HTTPRequest(#PB_HTTP_Get, URL, "") ; Initiating the HTTP request
If HTTPRequest
  Response = PeekS(HTTPMemory(HTTPRequest), -1, #PB_UTF8) ; Read HTTP response as a UTF-8 string
  Debug Response ; This will display the JSON response from the API
  FinishHTTP(HTTPRequest) ; Clean up the HTTP request
Else
  Debug "Error initializing HTTP request."
EndIf 
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Caronte3D
Addict
Addict
Posts: 1362
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Best way to identify the user's country of origin?

Post by Caronte3D »

The code I was posted get the info from the Windows it self, it's not to get the real country location :wink:
Axolotl
Addict
Addict
Posts: 838
Joined: Wed Dec 31, 2008 3:36 pm

Re: Best way to identify the user's country of origin?

Post by Axolotl »

Well, as I understand it, that will never be very precise.
The user can set any language or region on his OS. He can be dialed in via VPN, etc.
Assuming that the computer is connected and dials directly into the internet, you could trace the route. Under Windows with e.g.

Code: Select all

tracert purebasic.fr 
Check the Result as follows:
The first line is the router, the second line is the provider. Here you could then determine the country via the TLD in the 5th column.
No idea whether this is useful.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Best way to identify the user's country of origin?

Post by Joubarbe »

Well, I guess that's a risk I'm willing to take. If they use a VPN, or if they changed the language of their OS, it makes sense that the link they're clicking on respects their choice. If you live in France and you have a VPN in London, I will show you amazon.co.uk, and I think it's what you want, or it's what you should expect.
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Best way to identify the user's country of origin?

Post by AZJIO »

Table

Code: Select all

CompilerSelect #PB_Compiler_OS
	CompilerCase #PB_OS_Windows
		Define *Lang
		If OpenLibrary(0, "kernel32.dll")
			*Lang = GetFunction(0, "GetUserDefaultUILanguage")
			If *Lang
				Debug CallFunctionFast(*Lang)
			EndIf
			CloseLibrary(0)
		EndIf
	CompilerCase #PB_OS_Linux
		If ExamineEnvironmentVariables()
			While NextEnvironmentVariable()
				If Left(EnvironmentVariableName(), 4) = "LANG"
					Debug Left(EnvironmentVariableValue(), 2)
					Break
				EndIf
			Wend
		EndIf
CompilerEndSelect
Post Reply