{"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>
\n
Geoip.inc<\/a>
\n
Geoipcity.php<\/a>
\n
Geoipregionvars.php<\/a><\/p>\n

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

\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

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

\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

The results should output something like this:<\/p>\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

I ran the above code using a Memphis based proxy. As you can see MaxMinds database gives us:
\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

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

\r\necho $rsGeoData->city; \r\necho ' ,';\r\necho $rsGeoData->region;\r\n<\/pre>\n

Would out put: Memphis, TN.
\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&#91;'REMOTE_ADDR'&#93;);\r\ngeoip_close($gi);\r\n\r\n$location =  $rsGeoData->city.','.$rsGeoData->region;\r\n\r\n?>\r\n<\/pre>\n

Then in your template wherever you want the City, State to display simple:<\/p>\n

\r\n\r\n<h1>Meet girls near <?php echo $location;?><\/h1>\r\n\r\n\r\n<\/pre>\n

The your site might look something like this:
\n\"php<\/p>\n","protected":false},"excerpt":{"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 … 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":"\nHow to Get a Users Geo Location?<\/title>\n<meta name=\"description\" content=\"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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Johnathan Ward\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/phpgeocoding.jpg\",\"contentUrl\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/phpgeocoding.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#webpage\",\"url\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location\",\"name\":\"How to Get a Users Geo Location?\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#primaryimage\"},\"datePublished\":\"2009-02-23T00:27:29+00:00\",\"dateModified\":\"2009-03-04T18:54:53+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\"},\"description\":\"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\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Get a Users Geo Location?\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\",\"name\":\"Johnathan Ward\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"caption\":\"Johnathan Ward\"},\"description\":\"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.\",\"sameAs\":[\"http:\/\/johnathanward.com\",\"https:\/\/twitter.com\/spyderman4g63\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Get a Users Geo Location?","description":"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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location","twitter_misc":{"Written by":"Johnathan Ward","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#primaryimage","inLanguage":"en-US","url":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/phpgeocoding.jpg","contentUrl":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/phpgeocoding.jpg"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#webpage","url":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location","name":"How to Get a Users Geo Location?","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#primaryimage"},"datePublished":"2009-02-23T00:27:29+00:00","dateModified":"2009-03-04T18:54:53+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9"},"description":"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","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-get-a-users-geo-location#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"How to Get a Users Geo Location?"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9","name":"Johnathan Ward","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","caption":"Johnathan Ward"},"description":"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.","sameAs":["http:\/\/johnathanward.com","https:\/\/twitter.com\/spyderman4g63"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1767"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=1767"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1767\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}