{"id":14712,"date":"2013-05-20T06:41:06","date_gmt":"2013-05-20T06:41:06","guid":{"rendered":"http:\/\/www.adviceinteractivegroup.com\/?p=5414"},"modified":"2013-05-20T06:41:06","modified_gmt":"2013-05-20T06:41:06","slug":"how-to-display-unique-content-based-on-geolocation","status":"publish","type":"post","link":"https:\/\/al.rocksdmdev.com\/?p=14712","title":{"rendered":"How to Display Unique Content Based on Geolocation"},"content":{"rendered":"<h2>Unique Content and Geolocation<\/h2>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignright wp-image-12573 size-medium\" title=\"Unique Content and Geolocation\" src=\"\/images\/Unique-Content-and-Geolocation.jpg\" alt=\"Unique Content and Geolocation\" width=\"235\" height=\"300\" \/>Wikipedia defines <strong>geolocation<\/strong> as &#8220;the identification of the real-world geographic location of an object, such as a radar, mobile phone or an Internet-connected computer terminal.&#8221; Obviously in our example we will be referring to the Internet-connected computer terminal. While there is a simple way to establish geolocation by using the <a href=\"\/blog\/html5-and-the-geolocation-api\/\">Geolocation API (which we have already written about on this blog)<\/a>, it requires the visitor through a browser prompt to allow the website to access that information. Furthermore, the Geolocation API is only available to Internet Explorer 9 and up. With our more compatible method, we are able to skip the prompt and automatically implement our unique data. This tutorial will teach you how to serve unique content to visitors within a 50 mile radius of a certain city. We will use PHP and JSON but will not require intimate knowledge of either.<\/p>\n<p>First and foremost, we need to find our visitor&#8217;s IP address which we can do with this simple request:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n&lt;?php $ip = $_SERVER[&#8216;REMOTE_ADDR&#8217;]; ?&gt;<br \/>\n[\/code]<\/p>\n<p>Next we need to use the IP address to find out more about our visitor. We can do that by using http:\/\/www.freegeoip.net\/, a public web service for searching geolocation of IP addresses and host names. We make a JSON request to them with the following code:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n&lt;?php<br \/>\n$url = &#8220;http:\/\/freegeoip.net\/json\/&#8221; . $ip;<br \/>\n$json = file_get_contents($url);<br \/>\n$data = json_decode($json, TRUE);<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>The file_get_contents($url) function gets the contents of the url, which is http:\/\/freegeoip.net\/json\/whatever-ip-address-our-visitor-has. If you were to visit that website using your IP address you would see all sorts of nifty information about you, including your city, state, zipcode and more. Since it is listed in JSON format, we use the json_decode function to pass the data into a variable.<\/p>\n<p>Next, we need to pass our visitor&#8217;s city and state into variables:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n&lt;?php $city = print_r($data[&#8216;city&#8217;], true); ?&gt;<br \/>\n&lt;?php $state = print_r($data[&#8216;region_code&#8217;], true); ?&gt;<br \/>\n[\/code]<\/p>\n<p>Since the JSON data is in an array, we need to use the print_r() function to extract the individual city. Since we want to store the data in a variable, we use the return parameter by setting the second value of the print_r() function to true. At this point, you now have the city of your visitor stored in the variable $city. We also got the region code for the state because a lot of towns and cities do not have unique names (ie: Portland, Oregon and Portland, Maine).<\/p>\n<p><i>Note: The reason I cross reference by city, state is because in testing, those were always returning with a value. Zipcode was not. While I found it simpler to find all zipcodes in a radius, I had to go the extra step to ensure my script would always work. I was not able to find a faster, more accurate process to find all the cities, but if you know of one please write and let me know!<\/i> Now we need to get a list of all towns and cities in a 50 mile radius of our desired location. Our tutorial example will be Dallas, TX. First, lets get the zipcodes. There are several sites we can use, but I found a wonderful one at http:\/\/www.freemaptools.com\/find-zip-codes-inside-radius.htm which puts our zip codes in CSV format. We will go there and put in our Dallas zipcode &#8216;75001&#8217; and set the radius to 50 miles. This gives us <b>401 results<\/b>. Wow! Fortunately we do not have to look these cities up one zipcode at a time. I found another great website at http:\/\/pp19dd.com\/zipcode-lookup\/ to do it for us. Copy and paste those zipcodes into a text file and upload it to that website. It will give you a download of those zipcodes formatted in a .CSV document. Next, arrange those files in the .CSV to be all in one column. (I used this tutorial since I am no Excel wiz: http:\/\/office.microsoft.com\/en-us\/excel-help\/rotate-data-by-converting-columns-to-rows-or-vice-versa-HP005203138.aspx). After they are in one column, save it as a new file and upload that new file to the same website. You will receive an output file with cities attached to each of those zipcodes. Format that data (Delete duplicates, order A-&gt;Z, put on one line) and put it in your code as such:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n&lt;?php<br \/>\n$dallas_cities_in_50_mile_radius = &#8220;Addison Allen Alvarado Anna Argyle Arlington Aubrey Azle Bailey Balch Springs Bedford Benbrook Blue Ridge Boyd Burleson Caddo Mills Carrollton Carswell Afb Cedar Hill Celeste Celina Cockrell Hill Colleyville Collinsville Copeville Coppell Crandall Crowley Dallas De Soto Decatur Denton Duncanville Ennis Era Euless Everman Farmers Branch Farmersville Fate Ferris Flower Mound Forney Forreston Fort Worth Frisco Garland Grand Prairie Grapevine Greenville Gunter Haltom City Haslet Heath Highland Village Howe Hurst Hutchins Irving Josephine Joshua Justin Kaufman Keller Kennedale Krum Lake Dallas Lakewood Village Lancaster Lavon Leonard Lewisville Lillian Lindsay Mansfield Maypearl Mc Kinney Melissa Mesquite Midlothian Murphy Nevada Newark North Richland H Ovilla Palmer Pilot Point Plano Ponder Princeton Prosper Quinlan Randolph Rhome Rice Richardson River Oaks Rockwall Rowlett Royse City Sachse Saginaw Sanger Scurry Seagoville Sherman Southmayd Terrell The Colony Tioga Tom Bean Trenton Trophy Club Valley View Van Alstyne Venus Village Watauga Waxahachie Westminster White Settlement Whitewright Wilmer Wylie&#8221;;<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>Now we&#8217;re ready to cross reference our visitor&#8217;s city and state with our list of cities in the 50 mile radius. This is very simple:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n&lt;?php<br \/>\nif ((strpos($dallas_cities_in_50_mile_radius,$city) !== false) AND ($state == &#8220;TX&#8221;)) {<br \/>\necho &#8216;Visitor is within 50 miles of Dallas, TX&#8217;;<br \/>\n}<br \/>\nelse {<br \/>\necho &#8216;Visitor is not within 50 miles of Dallas, TX&#8217;;<br \/>\n}<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>Using the strpos() function, we reference the visitor&#8217;s location against our list of all towns and cities in the 50 mile radius as well as make sure their state is the same. We then echo out our unique content based on whether or not they fall within that 50 mile radius. Here is the code all together:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n&lt;?php<br \/>\n$ip = $_SERVER[&#8216;REMOTE_ADDR&#8217;];<br \/>\n$url = &#8220;http:\/\/freegeoip.net\/json\/&#8221; . $ip;<br \/>\n$json = file_get_contents($url);<br \/>\n$data = json_decode($json, TRUE);<br \/>\n$city = print_r($data[&#8216;city&#8217;], true);<br \/>\n$state = print_r($data[&#8216;region_code&#8217;], true);<br \/>\n$dallas_cities_in_50_mile_radius = &#8220;Addison Allen Alvarado Anna Argyle Arlington Aubrey Azle Bailey Balch Springs Bedford Benbrook Blue Ridge Boyd Burleson Caddo Mills Carrollton Carswell Afb Cedar Hill Celeste Celina Cockrell Hill Colleyville Collinsville Copeville Coppell Crandall Crowley Dallas De Soto Decatur Denton Duncanville Ennis Era Euless Everman Farmers Branch Farmersville Fate Ferris Flower Mound Forney Forreston Fort Worth Frisco Garland Grand Prairie Grapevine Greenville Gunter Haltom City Haslet Heath Highland Village Howe Hurst Hutchins Irving Josephine Joshua Justin Kaufman Keller Kennedale Krum Lake Dallas Lakewood Village Lancaster Lavon Leonard Lewisville Lillian Lindsay Mansfield Maypearl Mc Kinney Melissa Mesquite Midlothian Murphy Nevada Newark North Richland H Ovilla Palmer Pilot Point Plano Ponder Princeton Prosper Quinlan Randolph Rhome Rice Richardson River Oaks Rockwall Rowlett Royse City Sachse Saginaw Sanger Scurry Seagoville Sherman Southmayd Terrell The Colony Tioga Tom Bean Trenton Trophy Club Valley View Van Alstyne Venus Village Watauga Waxahachie Westminster White Settlement Whitewright Wilmer Wylie&#8221;;<\/p>\n<p>if ((strpos($dallas_cities_in_50_mile_radius,$city) !== false) AND ($state == &#8220;TX&#8221;)) {<br \/>\necho &#8216;Visitor is within 50 miles of Dallas, TX&#8217;;<br \/>\n}<br \/>\nelse {<br \/>\necho &#8216;Visitor is not within 50 miles of Dallas, TX&#8217;;<br \/>\n}<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>That is it!<\/p>\n<p><i>Note: The website http:\/\/freegeoip.net\/ is a free service and as such has a max 10,000 lookups across all users per hour. Fortunately it is open source and you can set it up on your webserver if you ever get the dreaded Error 403 Forbidden! The other free website tools I used to aid me in my tutorial were found with simple google searches. If for any reason these services cease to exist in the future, I have no doubt newer and better ones will replace them. You will just have to search for them. And I suppose a disclaimer is in line: IP address lookups are not perfect and will sometimes list inaccurate information. Using a 50 mile radius is a pretty big net however and will more than likely catch your visitor.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unique Content and Geolocation Wikipedia defines geolocation as &#8220;the identification of the real-world geographic location of an object, such as [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[9,17],"tags":[],"class_list":["post-14712","post","type-post","status-publish","format-standard","hentry","category-local-seo","category-web-development-design"],"_links":{"self":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/posts\/14712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14712"}],"version-history":[{"count":0,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/posts\/14712\/revisions"}],"wp:attachment":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}