Skip to content

Instantly share code, notes, and snippets.

@albertpak
Created December 19, 2013 20:43
Show Gist options
  • Select an option

  • Save albertpak/8045958 to your computer and use it in GitHub Desktop.

Select an option

Save albertpak/8045958 to your computer and use it in GitHub Desktop.
<?php
//a simple script to check user agents to detemine device type and or redirect
//==============================================
//get userangent string
$userAgentraw = $_SERVER['HTTP_USER_AGENT'];
//convert to lowercase to make search strings simplier
$userAgent = strtolower ( $userAgentraw );
//check string for key words
$ipad = strpos($userAgent, "ipad");
$android = strpos($userAgent, "android");
$msie10 = strpos($userAgent, "msie 10");
$msie8 = strpos($userAgent, "msie 8");
$touch = strpos($userAgent, "touch");
$mobile = strpos($userAgent, "mobile");
//double check windows tablet
if($msie10 == true && $touch == true){
$winTablet = true;
}else{
$winTablet = false;
}
//set urls for mobile & tablet
$tabeltURL = "t.siteurl.com";
$mobileURL = "m.siteurl.com";
//logic to determine device type
if ($mobile == true && $ipad == false) {
$deviceType = "mobile";
//header("location: $mobileURL");
} elseif ($ipad == true || $android == true || $winTablet == true){
$deviceType = "tablet";
//header("location: $tabeltURL");
}elseif ($msie8 == true){
$deviceType = "ie8";
}else{
$deviceType = "desktop";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment