WebHQ

The Headquarters Of Web

Facebook Twitter Google Plus RSS Feed Email

Auto Detecting PC or Mobile and then Redirecting - PHP code

Published on: at 2:52 PM | Posted By:
Most mobile websites make use of the HTTP header called “User-Agent”. This header tells the server what kind of web browser the user has. So, mobile websites usually look for the name of the device manufacturer in the User-Agent header. If it matches one of the manufacturers then the mobile optimized web page is sent.
  1. Create two folders in your server called "mobile" and "pc", each containing the respective versions of your site.
  2. Copy the code below.
  3. Replace the links ( http://YourSite.mobi/ ) with your Mobile and PC destinations.
  4. Save and upload as your index.php

Code

<?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');
   }
   
?>

1 comment On "Auto Detecting PC or Mobile and then Redirecting - PHP code"

  1. আপনারা সবাই কেমন আছেন । আশা করি ভালো আছেন । আজকে আমি আপনাদের সামনে রবি ফ্রী নেট বিভিন্ন উপায়ে ব্যবহারের দিক আলোচনা করব ।

    রবি ফ্রী নেট পিসির জন্যঃ
    Robi Free Net PC 2016
    রবি ফ্রী নেট অ্যান্ড্রয়েডের জন্যঃ
    Robi Free Net For Android 2016
    রবি ফ্রী নেট সকল মোবাইল জাভা এবং সেম্বিয়ানঃ
    Robi Free Net All Mobile ,Java , Symbian , Android

    ReplyDelete

Get Latest Updates

Blog Archive

Total Pageviews