GENTICS News
Mobile Detection durch PHP
Dieses Script haben wir dankenswerterweise im Internet gefunden und leicht modifiziert. Wir setzen es bereits in einigen Projekten ein, weil es trotz seiner Kürze sehr zuverlässig funktioniert.
<?php
$DOMAIN = '.gentics.com';
$WEB_DOMAIN = 'www' . $DOMAIN;
$MOBILE_DOMAIN = 'm' . $DOMAIN;
if ( $_GET['device'] )
{
gtx_redirect_device( $_GET['device'] );
}
function gtx_redirect_device( $device = 'www' )
{
if ( $device == 'mobile' )
{
setcookie ("DeviceCookie", "", time() - 3600, '/',' $DOMAIN');
unset($_COOKIE['DeviceCookie']);
setcookie("DeviceCookie", 1, time() + 43200, '/',' $DOMAIN');
//header("Location: ".$_SERVER['HTTP_REFERER']);
header("Location: http://" . $MOBILE_DOMAIN);
die();
} else {
setcookie ("DeviceCookie", "", time() - 3600, '/','$DOMAIN');
unset($_COOKIE['DeviceCookie']);
setcookie("DeviceCookie", 0, time() + 43200, '/','$DOMAIN');
//header("Location: ".$_SERVER['HTTP_REFERER']);
header("Location: http://" . $WEB_DOMAIN);
die();
}
}
function gtx_mobile_device_detect()
{
$useragent = $_SERVER['HTTP_USER_AGENT'];
$mobiledevice = 0;
$a = array('acs-','alav','alca','amoi','audi','aste','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','opwv','palm','pana','pant','pdxg','phil','play','pluc','port','prox','qtek','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','w3c ','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');
if (preg_match('/(ipod|iphone|android|opera mini|opera mobile|blackberry|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine|windows ce; ppc;|windows ce; smartphone;|windows ce; iemobile|up.browser|up.link|mmp|symbian|symbOS|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i',$useragent))
{
$mobiledevice = 1;
}
else if (stristr($_SERVER['HTTP_ACCEPT'],'text/vnd.wap.wml')||stristr($_SERVER['HTTP_ACCEPT'],'application/vnd.wap.xhtml+xml'))
{
$mobiledevice = 1;
}
else if (stristr($_SERVER['HTTP_ACCEPT'],'text/vnd.wap.wml')||stristr($_SERVER['HTTP_ACCEPT'],'application/vnd.wap.xhtml+xml'))
{
$mobiledevice = 1;
}
else if (isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])||isset($_SERVER['X-OperaMini-Features'])||isset($_SERVER['UA-pixels']))
{
$mobiledevice = 1;
}
else if ((strpos($accept,'text/vnd.wap.wml')>0)||(strpos($accept,'application/vnd.wap.xhtml+xml')>0))
{
$mobiledevice = 1;
}
else if (isset($a[substr($_SERVER['HTTP_USER_AGENT'],0,4)]))
{
$mobiledevice = 1;
}
else
{
$mobiledevice = 0;
}
return $mobiledevice;
}
if ($_COOKIE['DeviceCookie'] == false || !isset( $_COOKIE['DeviceCookie'] ) )
{
$mobile = gtx_mobile_device_detect();
setcookie("DeviceCookie", $mobile, time() + 43200, '/', '$DOMAIN');
} else
{
$mobile = $_COOKIE["DeviceCookie"];
}
// if device = desktop
if ($mobile == 0)
{
// if device = mobile
} else {
header("Location: http://$MOBILE_DOMAIN");
die();
}
?>