I use following code
<?php
$client = JFactory::getApplication()->client;
echo $client->browser; ?>
I test these codes in firefox,
It returns '18', not firefox. I want to show 'firefox'
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It only takes a minute to sign up.
Sign up to join this communityStrange, but Joomla\Application\Web\WebClient
class doesn't seem to hold information like that. Closest thing would be to get constant name using reflection:
$client = JFactory::getApplication()->client;
$reflection = new ReflectionClass($client);
$browser = array_search($client->browser, $reflection->getConstants());
echo $browser; // Outputs 'FIREFOX'
A simpler way is to use Joomla\CMS\Environment\Browser
class instead:
$client = JBrowser::getInstance();
echo $client->getBrowser(); // Outputs 'firefox'
I just called for the client
property from a Chrome browser like this:
JFactory::getApplication()->enqueueMessage(
var_export(JFactory::getApplication()->client, true)
);
and it provided the following output:
Joomla\Application\Web\WebClient::__set_state(
array(
'platform' => NULL,
'mobile' => false,
'engine' => NULL,
'browser' => 19,
'browserVersion' => '91.0.4472.106',
'languages' => array ( ),
'encodings' => array ( ),
'userAgent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
'acceptEncoding' => 'gzip, deflate, br',
'acceptLanguage' => 'en-US,en;q=0.9',
'robot' => false,
'detection' => array ( 'browser' => true, ),
'headers' => NULL,
)
)
You are seeing the constant value of Firefox with the 18
. You will need to translate that integer to its constant name. I recommend a lookup array then accessing the browser name string by its integer key in the generated array.
$browserLookup = array_flip((new ReflectionClass('JApplicationWebClient'))->getConstants());
JFactory::getApplication()->enqueueMessage(
$browserLookup[JFactory::getApplication()->client->browser] ?? 'Unknown'
);
// output: CHROME
Relevant links:
p.s. I agree with Sharky's recommended JBrowser::getInstance()->getBrowser()
approach; it is rather tidy and direct.
Here is what I found in the JBrowser instance when I called var_export()
it on my Chrome browser (on my old, junk computer):
Joomla\CMS\Environment\Browser::__set_state(array(
'majorVersion' => '91',
'minorVersion' => '0',
'browser' => 'chrome',
'agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
'lowerAgent' => 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/91.0.4472.106 safari/537.36',
'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'acceptParsed' =>
array (
),
'platform' => 'win',
'robots' =>
array (
0 => 'Googlebot',
1 => 'msnbot',
2 => 'Slurp',
3 => 'Yahoo',
4 => 'Arachnoidea',
5 => 'ArchitextSpider',
6 => 'Ask Jeeves',
7 => 'B-l-i-t-z-Bot',
8 => 'Baiduspider',
9 => 'BecomeBot',
10 => 'cfetch',
11 => 'ConveraCrawler',
12 => 'ExtractorPro',
13 => 'FAST-WebCrawler',
14 => 'FDSE robot',
15 => 'fido',
16 => 'geckobot',
17 => 'Gigabot',
18 => 'Girafabot',
19 => 'grub-client',
20 => 'Gulliver',
21 => 'HTTrack',
22 => 'ia_archiver',
23 => 'InfoSeek',
24 => 'kinjabot',
25 => 'KIT-Fireball',
26 => 'larbin',
27 => 'LEIA',
28 => 'lmspider',
29 => 'Lycos_Spider',
30 => 'Mediapartners-Google',
31 => 'MuscatFerret',
32 => 'NaverBot',
33 => 'OmniExplorer_Bot',
34 => 'polybot',
35 => 'Pompos',
36 => 'Scooter',
37 => 'Teoma',
38 => 'TheSuBot',
39 => 'TurnitinBot',
40 => 'Ultraseek',
41 => 'ViolaBot',
42 => 'webbandit',
43 => 'www.almaden.ibm.com/cs/crawler',
44 => 'ZyBorg',
),
'mobile' => false,
'images' =>
array (
0 => 'jpeg',
1 => 'gif',
2 => 'png',
3 => 'pjpeg',
4 => 'x-png',
5 => 'bmp',
),
))