Best way to identify the user's country of origin?
Best way to identify the user's country of origin?
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?
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?
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$
- NicTheQuick
- 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?
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.
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.
Re: Best way to identify the user's country of origin?
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?
)
Thank you NicTheQuick, I didn't know about that.
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$
Re: Best way to identify the user's country of origin?
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?
Ever is good practice to declare variables (and use EnableExplicit at first line of your codes) to get rid of bugs.Joubarbe wrote: Thu Apr 10, 2025 10:48 am Uhh... Would you mind explaining this code actually? Especially this first line...
Re: Best way to identify the user's country of origin?
Country code: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.
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)
Re: Best way to identify the user's country of origin?
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.
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
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
Re: Best way to identify the user's country of origin?
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?
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.
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
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).
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).
Re: Best way to identify the user's country of origin?
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?
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