/    Sign up×
Community /Pin to ProfileBookmark

How to get the browser’s location (state)?

Hello,
We need to get a really good idea of what state (of the U.S.) the visitor to our Web site is browsing from. We realize it can’t be perfect, but we just need to get a good idea. Our host’s logs/analytics isn’t that granular, so I’m hoping to add some scripting into our site to pull it.

Is there something better than $_SERVER[‘REMOTE_ADDR’], because that seems to give incorrect info a lot! We kind of want to get incorrect info just occasionally. ?

I see that Google Maps is doing something where they can locate you extremely accurately! I looked into it:
[URL=”https://code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation”]https://code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation[/URL]
but I can’t reverse engineer this so that all it does is output as a string the state. I don’t want the map, just to be able to pull the state so I can stick it into a database.

tl;dr: Can anyone point me to some example script that will allow me, with reasonable accuracy, to get the state of the U.S. the browser is in?

Thanks!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@swan52Nov 18.2011 — Unfortunately there is no way of knowing the users location from their browser information. People are 'located' by referencing a database somewhere that matches IP address of the user to a particular place. The locational data is only as good as the DB it is listed on and even then it can be innacurate, due to service providers that cover several states or even different countries sometimes having dynamically changing IP addresses making such users nearly impossible to locate.

Either way, you'll want yourself a database to reference, use one such as the above mentioned or make your own http://www.maxmind.com/app/geolitecity.
Copy linkTweet thisAlerts:
@007JulienNov 20.2011 — This script works without PHP
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fr">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>Untitled</title>
<style type="text/css">
</style>
</head>
<body>
<script language="JavaScript" src="http://j.maxmind.com/app/country.js"></script>
<br>Country Code:
<script language="JavaScript">document.write(geoip_country_code());</script>
<br>Country Name:
<script language="JavaScript">document.write(geoip_country_name());</script>

</body>
</html>[/code]

But you can too download the latest GeoLite Country CSV Format (Last updated November 1st, 2011, next update December 6th, 2011) to work with PHP on this page

This file looks like this :

"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"

"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"

"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"

"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"

"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"

"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"

"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"

"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"

"1.1.0.0","1.1.0.255","16842752","16843007","CN","China"

//...

And the two first data are the same as the two following

16777216=1*256*256*256 and 16777471=1*256*256*256+255

It means that IP between this two values are Australian IP adresses...

See too this page or this for US. Good luck !
×

Success!

Help @mechphisto spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...