Nowadays almost all people use the mobile devices to access the internet so now many of the websites you can see in responsive which detect the mobile device and load layout as per that.
Well,sometimes you want to load some PHP code only on mobile devices and there are multiple ways in a Joomla to respond a visitor when mobile device detected like many extensions available for free download that help to create mobile compatible version of a Joomla.
So, Today I am going to explain how to detect mobile device using Joomla libraries rather than using any extensions because of using Joomla we can take advantage of built-in browser detection class of Joomla.
First of all, if you are using any external file then you need to load Joomla framework inside that file
So ,let’s see the code:
1 2 3 4 5 6 7 8 | define( '_JEXEC', 1 ); define('JPATH_BASE', dirname(__FILE__)); define( 'DS', DIRECTORY_SEPARATOR ); require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' ); |
Now, first you need to use jimport() to get it into availability of the JBrowser class which is located in Joomla’s class library, in the environment sub package and then use the JBrowser class method in the Joomla framework to detect the browser.
1 2 3 | jimport( 'joomla.environment.browser' ); |
Then you need to get an instance of the browser object
1 2 3 4 5 6 7 8 9 10 11 | $browser = &JBrowser::getInstance(); echo 'Browser user-agent:' . $browser->getAgentString() . '<br/>'; echo 'Browser version: ' . $browser->getVersion() . '<br/>'; echo 'Browser information: ' . $browser->getBrowser() . '<br/>'; echo 'platform:' . $browser->getPlatform() . '<br/>'; if($browser->isMobile()) { //Your code place here } |
As you can see, it’s very simple.I hope this little tutorial helps you with how to use browser detection built-in library into Joomla.Again if you have any questions just drop me a comment. So that’s all for now.
Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (4)