{"id":446,"date":"2019-04-09T22:23:49","date_gmt":"2019-04-09T16:53:49","guid":{"rendered":"https:\/\/webdesignerinc.com\/blog\/?p=446"},"modified":"2019-04-09T22:24:36","modified_gmt":"2019-04-09T16:54:36","slug":"mobile-browser-detection-and-redirection-using-php","status":"publish","type":"post","link":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/","title":{"rendered":"Mobile Browser Detection and Redirection using PHP"},"content":{"rendered":"<p>This is a very simple script to detect the mobile browser and redirect the customers to the mobile version of the website. It is very easy to use and is of just few lines of code.<\/p>\n<p align=\"left\"><b>$_SERVER<\/b>\u00a0\u2013 an array containing information such as headers, paths, and script locations. (<a href=\"http:\/\/php.net\/manual\/en\/reserved.variables.server.php\" target=\"_blank\" rel=\"noopener noreferrer\">Know more<\/a>)<\/p>\n<p align=\"left\"><b>user_agent.php<\/b><br \/>\nCreate a file with the following script and name it as user_agent.php<\/p>\n<p align=\"left\"><span style=\"color: #3366ff;\"><strong>&lt;?php<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>$isMobile = (bool)preg_match(&#8216;#\\b(ip(hone|od|ad)|android|opera m(ob|in)i|windows (phone|ce)|blackberry|tablet&#8217; . &#8216;|s(ymbian|eries60|amsung)|p(laybook|alm|rofile\/midp|laystation portable)|nokia|fennec|htc[\\-_]&#8217; . &#8216;|mobile|up\\.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b#i&#8217;, $_SERVER[&#8216;HTTP_USER_AGENT&#8217;] );<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>if ($isMobile)<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>{<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>header(&#8216;Location: http:\/\/m.example.com \/&#8217;);<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>\/\/OR<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>echo &#8220;&lt;script&gt;window.location=&#8217;http:\/\/m.example.com'&lt;\/script&gt;&#8221;;<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>}<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>?&gt;<\/strong><\/span><\/p>\n<p align=\"left\">in index.php or home.php<br \/>\nYou have to include the above script like this.<\/p>\n<p align=\"left\"><span style=\"color: #3366ff;\"><strong>&lt;?php<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>include(&#8216;user_agent.php&#8217;); \u00a0\/\/ Redirecting http:\/\/m.example.com<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>\/\/ code for example.com page<\/strong><\/span><br \/>\n<span style=\"color: #3366ff;\"><strong>?&gt;<\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a very simple script to detect the mobile browser and redirect the customers to the mobile version of the website. It is very easy to use and is&#8230; <a class=\"read-more-link\" href=\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[25],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Mobile Browser Detection and Redirection using PHP - Web designer Blog | web designer<\/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:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mobile Browser Detection and Redirection using PHP - Web designer Blog | web designer\" \/>\n<meta property=\"og:description\" content=\"This is a very simple script to detect the mobile browser and redirect the customers to the mobile version of the website. It is very easy to use and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Web designer Blog | web designer\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Webdesignerinc\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-09T16:53:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-09T16:54:36+00:00\" \/>\n<meta name=\"author\" content=\"Teji\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@teji_tejinder\" \/>\n<meta name=\"twitter:site\" content=\"@teji_tejinder\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Teji\" \/>\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:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\"},\"author\":{\"name\":\"Teji\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7\"},\"headline\":\"Mobile Browser Detection and Redirection using PHP\",\"datePublished\":\"2019-04-09T16:53:49+00:00\",\"dateModified\":\"2019-04-09T16:54:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\"},\"wordCount\":180,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#organization\"},\"keywords\":[\"PHP\"],\"articleSection\":[\"Tutorial\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\",\"name\":\"Mobile Browser Detection and Redirection using PHP - Web designer Blog | web designer\",\"isPartOf\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#website\"},\"datePublished\":\"2019-04-09T16:53:49+00:00\",\"dateModified\":\"2019-04-09T16:54:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webdesignerinc.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mobile Browser Detection and Redirection using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#website\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/\",\"name\":\"Web designer Blog | web designer\",\"description\":\"Web Designer and Developer Blog\",\"publisher\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webdesignerinc.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#organization\",\"name\":\"Web designer Blog | web designer\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png\",\"contentUrl\":\"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png\",\"width\":409,\"height\":80,\"caption\":\"Web designer Blog | web designer\"},\"image\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Webdesignerinc\",\"https:\/\/x.com\/teji_tejinder\",\"http:\/\/www.linkedin.com\/in\/tejinder-singh-97bb19115\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7\",\"name\":\"Teji\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png\",\"caption\":\"Teji\"},\"description\":\"i am admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mobile Browser Detection and Redirection using PHP - Web designer Blog | web designer","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:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/","og_locale":"en_US","og_type":"article","og_title":"Mobile Browser Detection and Redirection using PHP - Web designer Blog | web designer","og_description":"This is a very simple script to detect the mobile browser and redirect the customers to the mobile version of the website. It is very easy to use and","og_url":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/","og_site_name":"Web designer Blog | web designer","article_publisher":"https:\/\/www.facebook.com\/Webdesignerinc","article_published_time":"2019-04-09T16:53:49+00:00","article_modified_time":"2019-04-09T16:54:36+00:00","author":"Teji","twitter_card":"summary_large_image","twitter_creator":"@teji_tejinder","twitter_site":"@teji_tejinder","twitter_misc":{"Written by":"Teji","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/#article","isPartOf":{"@id":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/"},"author":{"name":"Teji","@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7"},"headline":"Mobile Browser Detection and Redirection using PHP","datePublished":"2019-04-09T16:53:49+00:00","dateModified":"2019-04-09T16:54:36+00:00","mainEntityOfPage":{"@id":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/"},"wordCount":180,"commentCount":0,"publisher":{"@id":"https:\/\/webdesignerinc.com\/blog\/#organization"},"keywords":["PHP"],"articleSection":["Tutorial"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/","url":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/","name":"Mobile Browser Detection and Redirection using PHP - Web designer Blog | web designer","isPartOf":{"@id":"https:\/\/webdesignerinc.com\/blog\/#website"},"datePublished":"2019-04-09T16:53:49+00:00","dateModified":"2019-04-09T16:54:36+00:00","breadcrumb":{"@id":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webdesignerinc.com\/blog\/mobile-browser-detection-and-redirection-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webdesignerinc.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Mobile Browser Detection and Redirection using PHP"}]},{"@type":"WebSite","@id":"https:\/\/webdesignerinc.com\/blog\/#website","url":"https:\/\/webdesignerinc.com\/blog\/","name":"Web designer Blog | web designer","description":"Web Designer and Developer Blog","publisher":{"@id":"https:\/\/webdesignerinc.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webdesignerinc.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webdesignerinc.com\/blog\/#organization","name":"Web designer Blog | web designer","url":"https:\/\/webdesignerinc.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png","contentUrl":"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png","width":409,"height":80,"caption":"Web designer Blog | web designer"},"image":{"@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Webdesignerinc","https:\/\/x.com\/teji_tejinder","http:\/\/www.linkedin.com\/in\/tejinder-singh-97bb19115"]},{"@type":"Person","@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7","name":"Teji","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png","url":"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png","caption":"Teji"},"description":"i am admin"}]}},"_links":{"self":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/posts\/446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":0,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"wp:attachment":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}