hisorange / browser-detect
Browser & Mobile detection package for Laravel.
Installs: 6 882 556
Dependents: 60
Suggesters: 0
Security: 0
Stars: 1 094
Watchers: 29
Forks: 143
Open Issues: 13
Requires
- php: ^8.1
- jaybizzle/crawler-detect: ~1.2
- league/pipeline: ^1.0
- matomo/device-detector: ^6.0
- mobiledetect/mobiledetectlib: ~4.0
- ua-parser/uap-php: ~3.9
Requires (Dev)
- orchestra/testbench: ~7.0 || ~8.0
- php-coveralls/php-coveralls: ~2.0
- phpunit/phpunit: ~9.0 || ~10.0
- dev-stable
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.1
- 4.4.0
- 4.3.0
- 4.2.2
- 4.2.0
- 4.1.0
- 4.0.0
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.0.6
- 2.0.2
- 2.0.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 0.9.2
- 0.9.1
- dev-dependabot/composer/league/commonmark-2.6.0
- dev-dependabot/composer/symfony/var-dumper-6.4.15
- dev-dependabot/composer/laravel/framework-10.48.23
- dev-dependabot/composer/symfony/http-foundation-6.4.14
- dev-dependabot/composer/symfony/process-6.4.14
- dev-develop
- dev-backport/3.x
This package is auto-updated.
Last update: 2024-12-10 00:26:51 UTC
README
Browser Detection v5.0 by hisorange
Easy to use package to identify the visitor's browser details and device type. Magic is not involved the results are generated by multiple well tested and developed packages.
Supports every Laravel version between 4.0 » 10.x; Also tested on every PHP version between 5.6 » 8.2.
How to install
composer require hisorange/browser-detect
Yep, it's ready to be used by You! ^.^
How to use
In Your code just call the Browser facade:
use Browser; // Determine the user's device type is simple as this: $isMobile = Browser::isMobile(); Browser::isTablet(); Browser::isDesktop(); if (Browser::isMobile()) { // Redirect to the mobile version of the site. } // Every wondered if it is a bot who loading Your page? if (Browser::isBot()) { echo 'No need to wonder anymore!'; } // Check for common vendors. if (Browser::isFirefox() || Browser::isOpera()) { $response .= '<script src="firefox-fix.js"></script>'; } // Sometimes You may want to serve different content based on the OS. if (Browser::isAndroid()) { $response .= '<a>Install our Android App!</a>'; } elseif (Browser::isMac() && Browser::isMobile()) { $response .= '<a>Install our iOS App!</a>'; }
Even in Your blade templates:
@mobile <p>This is the MOBILE template!</p> @include('your-mobile-template') @endmobile @tablet <p>This is the TABLET template!</p> <link rel="stylesheet" href="tablet.css" title="Reduce the page size, load what the user need"> @endtablet @desktop <p>This is the DESKTOP template!</p> @enddesktop {-- Every result key is supported --} @browser('isBot') <p>Bots are identified too :)</p> @endbrowser
Easy peasy, ain't it?
Version support
The following matrix has been continuously tested by the great and awesome GitHub Actions!
Since 2013 the package runs tests on every possible PHP / Laravel version matrix.
Standalone mode, without Laravel!
Based on community requests; Now You can use the library without Laravel. Just simply use the Parser class as a static object.
use hisorange\BrowserDetect\Parser as Browser; if (Browser::isLinux()) { // Works as well! }
Available API calls
Every call on the Browser facade is proxied to a result object, so the following information are available on Your result too, where You can use the [array] syntax to access them.
Configuration, personalization
If You are using Laravel then after installation run the following command:
# Will copy a config file to ~/app/config/browser-detect.php
php artisan vendor:publish
For standalone mode to apply Your custom configuration:
use hisorange\BrowserDetect\Parser; $browser = new Parser(null, null, [ 'cache' => [ 'interval' => 86400 // This will override the default configuration. ] ]); $result = $browser->detect();
Since the package aims to be easy to use, there is not much to configure. But You can customize the cache and security values.
Advanced Usage Information
The code is designed to be an easy to use style, so every call You make on the Browser facade will access the result object and get the data for You, but You can parse agents other than the current user's.
// When You call the detect function You will get a result object, from the current user's agent. $result = Browser::detect(); // If You wanna get browser details from a user agent other than the current user call the parse function. $result = Browser::parse('Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14');
Worthy notion! The parser only parsing each user agent string once and then caches it, it uses an in-memory runtime cache for multiple calls in a single page load; And it will use the application's cache to persist the result for a week or so, this should provide You with a sufficient caching mechanism so the detection will cost less than 0.02 millisecond, this was tested with an 80,000 fake visit.
Changelog
See the detailed changes in the CHANGELOG file.