A PHP library to detect browser, OS, platform and device type by User-Agent parsing. This library requires
PHP 5.3 or higher. Installation by using ComposerInstall this library using Composer:
Update library package by running a command:
The first step requires the Composer autoloader: <?php require ('vendor/autoload.php'); // If you use PHP in Windows OS use another path style: // require ('vendor\autoload.php'); ?> UsageThe library will try to get environment data from the
First argument should contain User-Agent string from the <?php $Browser = new foroco\BrowserDetection(); $useragent = $_SERVER['HTTP_USER_AGENT']; // Get all possible environment data (array): $result = $Browser->getAll($useragent); // Get OS data (array): $result = $Browser->getOS($useragent); // Get Browser data (array): $result = $Browser->getBrowser($useragent); // Get Device type data (array): $result = $Browser->getDevice($useragent); /* Also methods may returned result as JSON string Just use second argument of methods as 'JSON' */ // Get all possible environment data (JSON): $result = $Browser->getAll($useragent, 'JSON'); print_r($result); // ... etc ?> The library class if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) { // Touch Event detected } Description for returned variablesOS Type (
OS
Family ( OS Name ( OS Version ( OS Title ( Device Type (
Browser Name ( Browser Version ( Browser Title ( Browser Chrome Original ( Browser Firefox Original ( Browser Safari Original ( Browser Chromium Version ( Browser Gecko Version ( Browser WebKit Version ( Browser Android Webview ( Browser iOS Webview ( Browser Desktop Mode ( 64 Bits Mode ( Usage ExamplesSee follow examples to understand library usage use cases. Detect AllTo detect all possible environment data use: <?php $Browser = new foroco\BrowserDetection(); $useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.4150.0 Iron Safari/537.36'; $result = $Browser->getAll($useragent); print_r($result); ?> Returns:
OS DetectionTo parse only OS data use: <?php $Browser = new foroco\BrowserDetection(); $useragent = 'Mozilla/5.0 (Linux; arm_64; Android 9; LLD-L31) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.136 YaBrowser/20.2.4.153.00 Mobile Safari/537.36'; $result = $Browser->getOS($useragent); print_r($result); ?> Returns:
Browser DetectionTo parse only browser data use: <?php $Browser = new foroco\BrowserDetection(); $useragent = 'Mozilla/5.0 (iPad; CPU OS 9_3_4 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/80.0.3987.122 Mobile/13G35 Safari/601.1.46'; $result = $Browser->getBrowser($useragent); print_r($result); ?> Returns:
Device DetectionTo parse only device type data use: <?php $Browser = new foroco\BrowserDetection(); $useragent = 'Mozilla/5.0 (SMART-TV; Linux; Tizen 5.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/2.2 Chrome/63.0.3239.84 TV Safari/537.36'; $result = $Browser->getDevice($useragent); print_r($result); ?> Returns:
Desktop Mode DetectionTo detect if mobile browser works in <?php $Browser = new foroco\BrowserDetection(); $useragent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'; $Browser->setTouchSupport(); // Call if Touch events detected in browser by JavaScript code ('ontouchstart' in window) $result = $Browser->getAll($useragent); print_r($result); ?> Returns:
Detect All (JSON)To pasre all possible environment data and returns JSON format string: <?php $Browser = new foroco\BrowserDetection(); $useragent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36'; $result = $Browser->getAll($useragent); print_r($result); ?> Returns:
Benchmarking TestBenchmarking was performed on a low-level shared hosting. User-Agent recognition performance in PHP 7.3 (requests per second):
LicenseThe MIT License (MIT) Copyright (c) 2020-2021 Artem Murugov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. How do I get user browser information?To detect user browser information we use the navigator. userAgent property. And then we match with the browser name to identify the user browser. Now call this JS function on page load, and this will display the user browser name on page load.
How can I get device detail in PHP?php $useragent=$_SERVER['HTTP_USER_AGENT']; if(preg_match('/(android|bb\d+|meego).. Unzip the file into a directory in your PHP server.. Then add the following code to your PHP page:. All available device information will be contained in $_51d array:. How do I get browser info in laravel?Getting the Browser
1$browser = Agent::browser(); In my particular use case, when I make the request, $browser is equal to Firefox . You can also find out the browser version by passing the result of Agent::browser() to the version() method like so: 1$browser = Agent::browser();
How do I find my browser name?Answer: To establish the actual name of the user's Web browser, you can use the navigator. appName and navigator. userAgent properties. The userAgent property is more reliable than appName because, for example, Firefox or Opera may return the string "Netscape" as the value of navigator.
|