Need to detect the mobile browser and load the mobile.css file for the Joomla model with PHP

advertisements

I need to detect if somebody is using a mobile browser (media queries aren't enough in this situation) via php and then load a mobile.css file to override a few lines of css due to lack of proper support for some code I'm using. I'm building a joomla template, so I need it to dynamically generate the path to the css file. So, am I doing this right? Example:

<?php
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $palmpre || $ipod || $berry == true)
{
echo "<link rel="stylesheet" href="$this->baseurl/templates/$this->template/css/mobile.css" type="text/css">";
}
?>


In Joomla there's mobile detection built in.

If you are using Joomla 3.0, you may use:

<?php
$templateUrl = $this->baseurl . '/templates/' . $this->template;
$doc         = JFactory::getDocument();
$appWeb      = new JApplicationWeb; // new JWebClient; (in Joomla 2.5)

if ($appWeb->client->mobile)
{
    $doc->addStyleSheet($templateUrl . '/css/mobile.css');
}
?>

in Joomla 2.5 or lower you'd use $appWeb = new JWebClient

in Joomla 3.4 you'll probably use if (JFactory::getApplication()->client->mobile)

I didn't find documentation page, but you may always inspect source code

By the way, check out responsive frameworks. It takes time to learn but it's worth to.