HOW TO REDIRECT YOUR SITE USING .PHP SCRIPT

It is impossible to create a website in less than 5mins. Building a site takes time and after the hard work of creating a desktop and mobile view of your website, you would want to redirect your readers to either the mobile or desktop view, depending on the device the reader is using. So how do you do this? I have dropped a post earlier on “HOW I REDIRECT MY MAIN SITE TO MY MOBILE SITE USING .HTACCESS” on our today issue, we are going to look how we can redirect our main site to our mobile site using .php script.

In case you don’t want to play around with your .htaccess then this tutorial suite you.

HOW YOU CAN REDIRECT YOU WEBSITE TO YOUR MOBILE SITE USING .PHP

Create 2 folders for your mobile and desktop website and upload your sites script to the folders

Open your notepad if you don’t have Dreamweaver and paste the below codes

<?php

$mobile_browser = ‘0’;

if(preg_match(‘/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i’,

strtolower($_SERVER[‘HTTP_USER_AGENT’]))){

$mobile_browser++;

}

if((strpos(strtolower($_SERVER[‘HTTP_ACCEPT’]),’application/vnd.wap.xhtml+xml’)>0) or

((isset($_SERVER[‘HTTP_X_WAP_PROFILE’]) or isset($_SERVER[‘HTTP_PROFILE’])))){

$mobile_browser++;

}

$mobile_ua = strtolower(substr($_SERVER[‘HTTP_USER_AGENT’],0,4));

$mobile_agents = array(

‘w3c ‘,’acs-‘,’alav’,’alca’,’amoi’,’audi’,’avan’,’benq’,’bird’,’blac’,

‘blaz’,’brew’,’cell’,’cldc’,’cmd-‘,’dang’,’doco’,’eric’,’hipt’,’inno’,

‘ipaq’,’java’,’jigs’,’kddi’,’keji’,’leno’,’lg-c’,’lg-d’,’lg-g’,’lge-‘,

‘maui’,’maxo’,’midp’,’mits’,’mmef’,’mobi’,’mot-‘,’moto’,’mwbp’,’nec-‘,

‘newt’,’noki’,’oper’,’palm’,’pana’,’pant’,’phil’,’play’,’port’,’prox’,

‘qwap’,’sage’,’sams’,’sany’,’sch-‘,’sec-‘,’send’,’seri’,’sgh-‘,’shar’,

‘sie-‘,’siem’,’smal’,’smar’,’sony’,’sph-‘,’symb’,’t-mo’,’teli’,’tim-‘,

‘tosh’,’tsm-‘,’upg1′,’upsi’,’vk-v’,’voda’,’wap-‘,’wapa’,’wapi’,’wapp’,

‘wapr’,’webc’,’winw’,’winw’,’xda’,’xda-‘);

if(in_array($mobile_ua,$mobile_agents)){

$mobile_browser++;

}

if (strpos(strtolower($_SERVER[‘ALL_HTTP’]),’OperaMini’)>0) {

$mobile_browser++;

}

if (strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’windows’)>0) {

$mobile_browser=0;

}

if($mobile_browser>0){

header(‘Location: http://YourSite.mobi/mobile’);

} else {

header(‘Location: http://YourSite.mobi/pc’);

}

?>

Have you copied the code above? Now save the code as index.php and upload it to your public_html

Now if you check your site with mobile it will redirect you to the mobile view and if you check with your pc it will redirect you to the desktop view.

If you got anything to add up then use the comment box below.

If you like this post, do click the like button and share us with friends. Don’t forget to subscribe to our feed for post on the go.

Leave a Comment