Back to Top

How to detect browser in Joomla

Browser Detection with Joomla

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:

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
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.

jimport( 'joomla.environment.browser' );

Then you need to get an instance of the browser object

$browser = &JBrowser::getInstance();
echo 'Browser user-agent:' . $browser->getAgentString() . '
'; echo 'Browser version: ' . $browser->getVersion() . '
'; echo 'Browser information: ' . $browser->getBrowser() . '
'; echo 'platform:' . $browser->getPlatform() . '
'; 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 (2)

  1. why not use this API : ? It’s much more detailed, accurate, up-to-date and faster. 😉

  2. I am not sure about this API and this is about how we use in built library provided by Joomla.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Ajax Synchronous and asynchronous requests

Posted on 2 years ago


how to use the CSS3 Gradient Effect

Posted on 6 years ago


How to use NoSQL and MongoDB

Posted on 7 years ago


The Reader’s Poll – June 2015

Posted on 3 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago