{"id":201,"date":"2016-09-03T23:29:29","date_gmt":"2016-09-03T17:59:29","guid":{"rendered":"http:\/\/hostmayo.com\/blog\/?p=201"},"modified":"2016-09-03T23:29:29","modified_gmt":"2016-09-03T17:59:29","slug":"know-country-visitor-php","status":"publish","type":"post","link":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/","title":{"rendered":"How To Get Country of Visitor in PHP"},"content":{"rendered":"<p>The title of our posts is <strong>&#8220;How To Get Country of Visitor in PHP<\/a>&#8221; <\/strong>however our actual mission here is to get to know the country of our visitor and redirect him to another page\/domain or url in php.<\/p>\n<p>The simplest way to do it in php is as follow:<\/p>\n<ol>\n<li>Download the following class or file in layman&#8217;s term from the below url<\/li>\n<\/ol>\n<div class=\"post-text\">\n<p>http:\/\/www.geoplugin.com\/_media\/webservices\/geoplugin.class.phps<\/p>\n<p>2. Upload it on your server and then create a new file in the same directory. Lets name the new file index.php.<\/p>\n<p>3. Paste the following code in the index.php<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code><span class=\"pun\">&lt;?<\/span><span class=\"pln\">php\r\nrequire_once<\/span><span class=\"pun\">(<\/span><span class=\"str\">'geoplugin.class.php'<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\r\n$geoplugin <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span><span class=\"pln\"> geoPlugin<\/span><span class=\"pun\">();<\/span><span class=\"pln\">\r\n$geoplugin<\/span><span class=\"pun\">-&gt;<\/span><span class=\"pln\">locate<\/span><span class=\"pun\">();<\/span>\r\n<span class=\"com\">\/\/ create a variable for the country code<\/span><span class=\"pln\">\r\n$var_country_code <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> $geoplugin<\/span><span class=\"pun\">-&gt;<\/span><span class=\"pln\">countryCode<\/span><span class=\"pun\">;<\/span>\r\n<span class=\"com\">\/\/ redirect based on country code:<\/span>\r\n<span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">$var_country_code <\/span><span class=\"pun\">==<\/span> <span class=\"str\">\"PK\"<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\nheader<\/span><span class=\"pun\">(<\/span><span class=\"str\">'Location: https:\/\/hostmayo.com\/pk\/'<\/span><span class=\"pun\">);<\/span>\r\n<span class=\"pun\">}<\/span>\r\n<span class=\"kwd\">else<\/span> <span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">$var_country_code <\/span><span class=\"pun\">==<\/span> <span class=\"str\">\"US\"<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\nheader<\/span><span class=\"pun\">(<\/span><span class=\"str\">'Location: https:\/\/hostmayo.com\/'<\/span><span class=\"pun\">);<\/span>\r\n<span class=\"pun\">}<\/span>\r\n<span class=\"kwd\">else<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\nheader<\/span><span class=\"pun\">(<\/span><span class=\"str\">'Location: http:\/\/en.wikipedia.org\/'<\/span><span class=\"pun\">);<\/span>\r\n<span class=\"pun\">}<\/span>\r\n<span class=\"pun\">?&gt;\r\n4. Just change the locations starting with https to what ever place you want to redirect the user to. \r\n5. The list of country codes used in the above scripts can be found in the list here\r\n<\/span><\/code><\/pre>\n<p><a href=\"http:\/\/www.geoplugin.com\/iso3166\"><strong>http:\/\/www.geoplugin.com\/iso3166<\/strong><\/a><\/p>\n<p>6. We also have enclosed the both files with this post. However the test.php in enclosed package just tells you the country code. It is just to see if your script is working fine.<\/p>\n<h3>EXTRA TIP:<\/h3>\n<p>You could do this without require_once(&#8216;geoplugin.class.php&#8217;); like so:<\/p>\n<p>&lt;?php<br \/>\n$a = unserialize(file_get_contents(&#8216;http:\/\/www.geoplugin.net\/php.gp?ip=&#8217;.$_SERVER[&#8216;REMOTE_ADDR&#8217;]));<br \/>\n$countrycode= $a[&#8216;geoplugin_countryCode&#8217;];<br \/>\nif ($countrycode==&#8217;US&#8217;)<br \/>\nheader( &#8216;Location: http:\/\/example1.com&#8217; ) ;<br \/>\nelse<br \/>\nheader( &#8216;Location: http:\/\/example2.com&#8217; ) ;<\/p>\n<p>?&gt;<\/p>\n<p><a href=\"http:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/countryscript.zip\">Download countryscript<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The title of our posts is &#8220;How To Get Country of Visitor in PHP&#8221; however our actual mission here is to get to know the [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Get Country of Visitor in PHP<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Get Country of Visitor in PHP\" \/>\n<meta property=\"og:description\" content=\"The title of our posts is &#8220;How To Get Country of Visitor in PHP&#8221; however our actual mission here is to get to know the [&hellip;] Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Host Mayo\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Hostmayoservers\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-03T17:59:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"570\" \/>\n\t<meta property=\"og:image:height\" content=\"240\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Billa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hostmayo\" \/>\n<meta name=\"twitter:site\" content=\"@hostmayo\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Billa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\"},\"author\":{\"name\":\"Billa\",\"@id\":\"https:\/\/hostmayo.com\/blog\/#\/schema\/person\/a511fa95af50c3cb2614311b73c2ea9d\"},\"headline\":\"How To Get Country of Visitor in PHP\",\"datePublished\":\"2016-09-03T17:59:29+00:00\",\"dateModified\":\"2016-09-03T17:59:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\"},\"wordCount\":203,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg\",\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\",\"url\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\",\"name\":\"How To Get Country of Visitor in PHP\",\"isPartOf\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg\",\"datePublished\":\"2016-09-03T17:59:29+00:00\",\"dateModified\":\"2016-09-03T17:59:29+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage\",\"url\":\"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg\",\"contentUrl\":\"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg\",\"width\":570,\"height\":240,\"caption\":\"How To Get Country of Visitor in PHP\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/hostmayo.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Get Country of Visitor in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/hostmayo.com\/blog\/#website\",\"url\":\"https:\/\/hostmayo.com\/blog\/\",\"name\":\"Host Mayo\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/hostmayo.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/hostmayo.com\/blog\/#organization\",\"name\":\"Host Mayo\",\"url\":\"https:\/\/hostmayo.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/hostmayo.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2015\/12\/ico.png\",\"contentUrl\":\"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2015\/12\/ico.png\",\"width\":64,\"height\":64,\"caption\":\"Host Mayo\"},\"image\":{\"@id\":\"https:\/\/hostmayo.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Hostmayoservers\",\"https:\/\/x.com\/hostmayo\",\"https:\/\/www.linkedin.com\/company\/host-mayo\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/hostmayo.com\/blog\/#\/schema\/person\/a511fa95af50c3cb2614311b73c2ea9d\",\"name\":\"Billa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/hostmayo.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/55380a32a6c37aba9b35b3d51dbd6cd6460322a433ecdaaacb28aece47163e51?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/55380a32a6c37aba9b35b3d51dbd6cd6460322a433ecdaaacb28aece47163e51?s=96&d=mm&r=g\",\"caption\":\"Billa\"},\"description\":\"Geek, Banker &amp; Entrepreneur.\",\"sameAs\":[\"https:\/\/hostmayo.com\/\"],\"url\":\"https:\/\/hostmayo.com\/blog\/author\/waqass\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Get Country of Visitor in PHP","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:\/\/hostmayo.com\/blog\/know-country-visitor-php\/","og_locale":"en_US","og_type":"article","og_title":"How To Get Country of Visitor in PHP","og_description":"The title of our posts is &#8220;How To Get Country of Visitor in PHP&#8221; however our actual mission here is to get to know the [&hellip;] Read More","og_url":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/","og_site_name":"Host Mayo","article_publisher":"https:\/\/www.facebook.com\/Hostmayoservers","article_published_time":"2016-09-03T17:59:29+00:00","og_image":[{"width":570,"height":240,"url":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg","type":"image\/jpeg"}],"author":"Billa","twitter_card":"summary_large_image","twitter_creator":"@hostmayo","twitter_site":"@hostmayo","twitter_misc":{"Written by":"Billa","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#article","isPartOf":{"@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/"},"author":{"name":"Billa","@id":"https:\/\/hostmayo.com\/blog\/#\/schema\/person\/a511fa95af50c3cb2614311b73c2ea9d"},"headline":"How To Get Country of Visitor in PHP","datePublished":"2016-09-03T17:59:29+00:00","dateModified":"2016-09-03T17:59:29+00:00","mainEntityOfPage":{"@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/"},"wordCount":203,"commentCount":0,"publisher":{"@id":"https:\/\/hostmayo.com\/blog\/#organization"},"image":{"@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage"},"thumbnailUrl":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg","articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/","url":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/","name":"How To Get Country of Visitor in PHP","isPartOf":{"@id":"https:\/\/hostmayo.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage"},"image":{"@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage"},"thumbnailUrl":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg","datePublished":"2016-09-03T17:59:29+00:00","dateModified":"2016-09-03T17:59:29+00:00","breadcrumb":{"@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#primaryimage","url":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg","contentUrl":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg","width":570,"height":240,"caption":"How To Get Country of Visitor in PHP"},{"@type":"BreadcrumbList","@id":"https:\/\/hostmayo.com\/blog\/know-country-visitor-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/hostmayo.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Get Country of Visitor in PHP"}]},{"@type":"WebSite","@id":"https:\/\/hostmayo.com\/blog\/#website","url":"https:\/\/hostmayo.com\/blog\/","name":"Host Mayo","description":"","publisher":{"@id":"https:\/\/hostmayo.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/hostmayo.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/hostmayo.com\/blog\/#organization","name":"Host Mayo","url":"https:\/\/hostmayo.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/hostmayo.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2015\/12\/ico.png","contentUrl":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2015\/12\/ico.png","width":64,"height":64,"caption":"Host Mayo"},"image":{"@id":"https:\/\/hostmayo.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Hostmayoservers","https:\/\/x.com\/hostmayo","https:\/\/www.linkedin.com\/company\/host-mayo"]},{"@type":"Person","@id":"https:\/\/hostmayo.com\/blog\/#\/schema\/person\/a511fa95af50c3cb2614311b73c2ea9d","name":"Billa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/hostmayo.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/55380a32a6c37aba9b35b3d51dbd6cd6460322a433ecdaaacb28aece47163e51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/55380a32a6c37aba9b35b3d51dbd6cd6460322a433ecdaaacb28aece47163e51?s=96&d=mm&r=g","caption":"Billa"},"description":"Geek, Banker &amp; Entrepreneur.","sameAs":["https:\/\/hostmayo.com\/"],"url":"https:\/\/hostmayo.com\/blog\/author\/waqass\/"}]}},"jetpack_featured_media_url":"https:\/\/hostmayo.com\/blog\/wp-content\/uploads\/2016\/09\/geo-logo.jpg","_links":{"self":[{"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":0,"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/media\/202"}],"wp:attachment":[{"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostmayo.com\/blog\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}