1# cbschuld/browser.php 2 3[](https://github.com/cbschuld/Browser.php/actions/workflows/tests.yml) 4 5Helps detect the user's browser and platform at the PHP level via the user agent 6 7 8## Installation 9 10### Via Composer (Recommended) 11 12```bash 13composer require cbschuld/browser.php 14``` 15 16For development only: 17```bash 18composer require --dev cbschuld/browser.php 19``` 20 21### Direct Download 22 23Download `Browser.php` and include it in your project: 24```php 25require_once 'Browser.php'; 26``` 27 28## Usage 29 30### Modern Usage (v2.0+, Recommended) 31 32```php 33use cbschuld\Browser; 34 35$browser = new Browser(); 36if ($browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->compareVersion('10', '>=')) { 37 echo 'You have Firefox version 10 or greater'; 38} 39``` 40 41### Legacy Usage (Still Supported) 42 43```php 44// Works via automatic aliasing - no namespace needed 45$browser = new Browser(); 46if ($browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 10) { 47 echo 'You have Firefox version 10 or greater'; 48} 49``` 50 51### Version Comparison (PHP 8+ Compatible) 52 53```php 54use cbschuld\Browser; 55 56$browser = new Browser(); 57 58// Recommended approach for version comparison 59if ($browser->compareVersion('15.0', '>=')) { 60 echo 'Modern browser detected'; 61} 62 63// Multiple comparisons 64if ($browser->getBrowser() == Browser::BROWSER_CHROME && 65 $browser->compareVersion('90', '>=')) { 66 echo 'Modern Chrome detected'; 67} 68``` 69 70## Browser Detection 71 72This solution identifies the following Browsers and does a best-guess on the version: 73 74* Opera (`Browser::BROWSER_OPERA`) 75* WebTV (`Browser::BROWSER_WEBTV`) 76* NetPositive (`Browser::BROWSER_NETPOSITIVE`) 77* Edge (`Browser::BROWSER_EDGE`) 78* Internet Explorer (`Browser::BROWSER_IE`) 79* Pocket Internet Explorer (`Browser::BROWSER_POCKET_IE`) 80* Galeon (`Browser::BROWSER_GALEON`) 81* Konqueror (`Browser::BROWSER_KONQUEROR`) 82* iCab (`Browser::BROWSER_ICAB`) 83* OmniWeb (`Browser::BROWSER_OMNIWEB`) 84* Phoenix (`Browser::BROWSER_PHOENIX`) 85* Firebird (`Browser::BROWSER_FIREBIRD`) 86* UCBrowser (`Browser::BROWSER_UCBROWSER`) 87* Firefox (`Browser::BROWSER_FIREFOX`) 88* Mozilla (`Browser::BROWSER_MOZILLA`) 89* Palemoon (`Browser::BROWSER_PALEMOON`) 90* curl (`Browser::BROWSER_CURL`) 91* wget (`Browser::BROWSER_WGET`) 92* Amaya (`Browser::BROWSER_AMAYA`) 93* Lynx (`Browser::BROWSER_LYNX`) 94* Safari (`Browser::BROWSER_SAFARI`) 95* Playstation (`Browser::BROWSER_PLAYSTATION`) 96* iPhone (`Browser::BROWSER_IPHONE`) 97* iPod (`Browser::BROWSER_IPOD`) 98* Google's Android(`Browser::BROWSER_ANDROID`) 99* Google's Chrome(`Browser::BROWSER_CHROME`) 100* GoogleBot(`Browser::BROWSER_GOOGLEBOT`) 101* Yahoo!'s Slurp(`Browser::BROWSER_SLURP`) 102* W3C's Validator(`Browser::BROWSER_W3CVALIDATOR`) 103* BlackBerry(`Browser::BROWSER_BLACKBERRY`) 104 105## Operating System Detection 106 107This solution identifies the following Operating Systems: 108 109* Windows (`Browser::PLATFORM_WINDOWS`) 110* Windows CE (`Browser::PLATFORM_WINDOWS_CE`) 111* Apple (`Browser::PLATFORM_APPLE`) 112* Linux (`Browser::PLATFORM_LINUX`) 113* Android (`Browser::PLATFORM_ANDROID`) 114* OS/2 (`Browser::PLATFORM_OS2`) 115* BeOS (`Browser::PLATFORM_BEOS`) 116* iPhone (`Browser::PLATFORM_IPHONE`) 117* iPod (`Browser::PLATFORM_IPOD`) 118* BlackBerry (`Browser::PLATFORM_BLACKBERRY`) 119* FreeBSD (`Browser::PLATFORM_FREEBSD`) 120* OpenBSD (`Browser::PLATFORM_OPENBSD`) 121* NetBSD (`Browser::PLATFORM_NETBSD`) 122* SunOS (`Browser::PLATFORM_SUNOS`) 123* OpenSolaris (`Browser::PLATFORM_OPENSOLARIS`) 124* iPad (`Browser::PLATFORM_IPAD`) 125 126## History and Legacy 127 128Detecting the user's browser type and version is helpful in web applications that harness some of the newer bleeding edge concepts. With the browser type and version you can notify users about challenges they may experience and suggest they upgrade before using such application. Not a great idea on a large scale public site; but on a private application this type of check can be helpful. 129 130In an active project of mine we have a pretty graphically intensive and visually appealing user interface which leverages a lot of transparent PNG files. Because we all know how great IE6 supports PNG files it was necessary for us to tell our users the lack of power their browser has in a kind way. 131 132Searching for a way to do this at the PHP layer and not at the client layer was more of a challenge than I would have guessed; the only script available was written by Gary White and Gary no longer maintains this script because of reliability. I do agree 100% with Gary about the readability; however, there are realistic reasons to desire the user's browser and browser version and if your visitor is not echoing a false user agent we can take an educated guess. 133 134I based this solution off of Gary White's original work but have since replaced all of his original code. Either way, thank you to Gary. Sadly, I never was able to get in touch with him regarding this solution. 135 136## Testing 137 138The testing with PHPUnit against known user agents available in tests/lists. Each file is tab delimited with the following fields: 139 140User Agent, User Agent Type, Browser, Version, Operating System, Operating System Version 141 142eg 143``` 144Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Browser Opera 12.16 Linux Linux 145Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 Browser Chrome 14.0.835.186 Macintosh OS X 10_7_2 146``` 147 148Tests can be run by phpunit: 149 150```bash 151vendor/bin/phpunit 152``` 153 154## Upgrading from v1.x 155 156See [UPGRADING.md](UPGRADING.md) for detailed migration instructions. 157 158**Quick Summary:** 159- v2.0 requires PHP 8.0+ 160- Class is now namespaced: `cbschuld\Browser` 161- Backward compatibility maintained via automatic aliasing 162- Use `compareVersion()` for reliable version comparisons 163 164 165