<?php
/*
* Class: mobiled
* Description: simple mobile browser detection (PHP 5)
* Author: Filip Oscadal <hide@address.com>, http://www.mxd.cz
* Version: 1.0.0 (2009-07-13)
* License: GNU General Public License
*/
class mobiled
{
private $mobile = 0;
private $version = array();
// call the detection and send headers
public function __construct()
{
$this->detect();
$this->header();
return ($this->mobile) ? $this->getVersion() : 0;
}
// detect a mobile browser by parsing $_SERVER variables
public function detect()
{
if (preg_match('/(android|avantgo|blackberry|blazer|elaine|hiptop|iphone|ipod|kindle|midp|mmp|mobile|o2|opera mini|palm|palm os|pda|plucker|pocket|psp|smartphone|symbian|treo|up.browser|up.link|vodafone|wap|windows ce; iemobile|windows ce; ppc;|windows ce; smartphone;|xiino)/i', $_SERVER['HTTP_USER_AGENT'], $this->version)) $this->mobile = 1;
if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) $this->mobile = 1;
if ((strpos($_SERVER['HTTP_ACCEPT'], 'text/vnd.wap.wml') > 0) || (strpos($_SERVER['HTTP_ACCEPT'], 'application/vnd.wap.xhtml+xml') > 0)) $this->mobile = 1;
return $this->mobile;
}
// send special HTTP headers
public function header()
{
if (($this->mobile) && (!headers_sent()))
{
// this directive indicates that (most of) the response must not be transformed
header('Cache-Control: no-transform');
header('Vary: User-Agent, Accept');
return true;
}
return false;
}
// return detected mobile version
public function getVersion()
{
if (isset($this->version[0]))
unset($this->version[0]);
return join('|', $this->version);
}
}
?>