Page 1 of 1
Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 9:55 am
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?
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 10:06 am
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$
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 10:32 am
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.
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 10:48 am
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?

)
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.
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 11:25 am
by Joubarbe
It would actually be good to obtain the
country code of the user. Instead of a string.
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 12:38 pm
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.
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 12:39 pm
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.
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 12:45 pm
by Joubarbe
Thanks!
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 1:55 pm
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
Re: Best way to identify the user's country of origin?
Posted: Thu Apr 10, 2025 2:01 pm
by Caronte3D
The code I was posted get the info from the Windows it self, it's not to get the real country location

Re: Best way to identify the user's country of origin?
Posted: Sat Apr 12, 2025 11:27 am
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.
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.
Re: Best way to identify the user's country of origin?
Posted: Sat Apr 12, 2025 11:43 am
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.
Re: Best way to identify the user's country of origin?
Posted: Sat Apr 12, 2025 4:22 pm
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