{"id":1767,"date":"2009-02-22T20:27:29","date_gmt":"2009-02-23T00:27:29","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1767"},"modified":"2009-03-04T14:54:53","modified_gmt":"2009-03-04T18:54:53","slug":"how-to-get-a-users-geo-location","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location","title":{"rendered":"How to Get a Users Geo Location?"},"content":{"rendered":"
Every wonder how all of those dating ads know that you are looking for hot girls near (insert your city here)? Well if you are a complete nerd like me you probably ignored the hot girls and wondered: \u201cHow did they know where I live?\u201d. There are a few simple ways to do this. Today I am going to show you how to GeoCode using MaxMind’s free version.<\/p>\n
MaxMind offers a paid version of this script that probably offers much more functionality. For what I need though, the free database does the job. I checked the results via a few proxies and they matched the US city fine. I am not sure how this works with international visitors, but I use it for US targeted traffic (It does pick up Canadian cities and Provinces fine\u2026.eh)..<\/p>\n
First off you are going to need to download the Database and PHP API code:<\/p>\n
MaxMind GeoLiteCity<\/a> Once you download all of the files. Extract them all and upload them to the same folder on your server.<\/p>\n Now to use the API we need to include a few files. Then we are going to open the database and check the users location:<\/p>\n This will grab the information, but what can we do with it. To find out what variables you can use print out the array:<\/p>\n The results should output something like this:<\/p>\n I ran the above code using a Memphis based proxy. As you can see MaxMinds database gives us: Ok so now that we know what everything is, all we have to do is echo the results in the proper places. For example:<\/p>\n Would out put: Memphis, TN. Then in your template wherever you want the City, State to display simple:<\/p>\n The your site might look something like this: Every wonder how all of those dating ads know that you are looking for hot girls near (insert your city here)? Well if you are a complete nerd like me you probably ignored the hot girls and wondered: \u201cHow did they know where I live?\u201d. There are a few simple ways to do this. Today … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,32,17],"tags":[78,81,80,79,47],"yoast_head":"\n
\nGeoip.inc<\/a>
\nGeoipcity.php<\/a>
\nGeoipregionvars.php<\/a><\/p>\n\r\ninclude("geoip.inc");\r\ninclude("geoipcity.inc");\r\ninclude("geoipregionvars.php");\r\n$gi = geoip_open(".\/GeoLiteCity.dat", GEOIP_STANDARD);\r\n$rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);\r\ngeoip_close($gi);\r\n<\/pre>\n
\r\ninclude("geoip.inc");\r\ninclude("geoipcity.inc");\r\ninclude("geoipregionvars.php");\r\n$gi = geoip_open(".\/GeoLiteCity.dat", GEOIP_STANDARD);\r\n$rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);\r\ngeoip_close($gi);\r\n\r\nprint "<pre>";\r\nprint_r($rsGeoData);\r\nprint "<\/pre>";\r\n<\/pre>\n
\r\ngeoiprecord Object\r\n(\r\n [country_code] => US\r\n [country_code3] => USA\r\n [country_name] => United States\r\n [region] => TN\r\n [city] => Memphis\r\n [postal_code] => \r\n [latitude] => 35.1242\r\n [longitude] => -89.9521\r\n [area_code] => 901\r\n [dma_code] => 640\r\n)\r\n<\/pre>\n
\nCountry Abbreviation in two formats,
\nFull country name,
\nRegion (state\/province),
\nCity,
\nPostal code (this usually works but is blank in the example),
\nLatitude,
\nLongitude,
\nArea Code,
\nDMA code (Designated Market Area as in Nielsen Media tv\/radio market areas in the US)<\/p>\n\r\necho $rsGeoData->city; \r\necho ' ,';\r\necho $rsGeoData->region;\r\n<\/pre>\n
\nSo if you were going to use this in a template you would use something like this in the header:<\/p>\n\r\n<?php\r\ninclude("geoip.inc");\r\ninclude("geoipcity.inc");\r\ninclude("geoipregionvars.php");\r\n$gi = geoip_open(".\/GeoLiteCity.dat", GEOIP_STANDARD);\r\n$rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);\r\ngeoip_close($gi);\r\n\r\n$location = $rsGeoData->city.','.$rsGeoData->region;\r\n\r\n?>\r\n<\/pre>\n
\r\n\r\n<h1>Meet girls near <?php echo $location;?><\/h1>\r\n\r\n\r\n<\/pre>\n
\n<\/p>\n","protected":false},"excerpt":{"rendered":"