PHP User Agent Parser
- Comments:
- 35
Requirements
- PHP >= 5.4.0
Download
The script is available over on GitHub or via Composer:
$ composer require 'donatj/phpuseragentparser'
Usage
The original procedural use is as simple as:
<?php
// v0 style global function - @deprecated
$uaInfo = parse_user_agent();
// or
// modern namespaced function
$uaInfo = donatj\UserAgent\parse_user_agent();
echo $uaInfo[donatj\UserAgent\PLATFORM] . PHP_EOL;
echo $uaInfo[donatj\UserAgent\BROWSER] . PHP_EOL;
echo $uaInfo[donatj\UserAgent\BROWSER_VERSION] . PHP_EOL;
The new object oriented wrapper form:
<?php
use donatj\UserAgent\UserAgentParser;
$parser = new UserAgentParser();
// object-oriented call
$ua = $parser->parse();
// or
// command style invocation
$ua = $parser();
echo $ua->platform() . PHP_EOL;
echo $ua->browser() . PHP_EOL;
echo $ua->browserVersion() . PHP_EOL;
Currently Detected Platforms
see: List of Detected Browsers
Comment by: Valery on
For example WinXP, Win7, Win8, etc.
And processor detail 32/64 bit.
Comment by: RuuRd on
if( $keys = array_intersect($priority, $result['platform']) ) {
assignment to $key instead of evaluation:
if( ( $data['platform'] == 'Android' && !($key = 0) ) ||
some $key = array_search() evaluation to “!== false” seem to be missing ..
like
elseif( $key = array_search( 'Safari', $result['browser'] ) ) {
Your function works well though!
Comment by: Jesse G. Donat on
Comment by: Tomas on
[2013-07-09 23:39:06] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 241. Browser: v (Unknown) 92.243.10.170
[2013-07-09 23:39:06] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 242. Browser: v (Unknown) 92.243.10.170
[2013-07-09 23:42:30] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 257. Browser: v (Unknown) 92.243.10.170
[2013-07-09 23:42:30] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 275. Browser: v (Unknown) 92.243.10.170
first two errors:
$data['browser'] = $result['browser'][0];
$data['version'] = $result['version'][0];
and others in:
} elseif($result['browser'][0] == 'AppleWebKit') {
} elseif($result['browser'][0] == 'MSIE') {
Comment by: Jesse G. Donat on
Considering hiding notices using http://php.net/manual/en/function.error-reporting.php
Comment by: farzin on
hi , i have failed to use your script.
i have uploaded the whole github directory.
Then i'm running UserAgentParserTest.php
and i get nothing.
i use this code in another php and i get nothing again :(
require 'UserAgentParser.php';
$ua_info = parse_user_agent();
print_r($ua_info);
echo "farzin".$ua_info;
Comment by: farzin on
i finally got it working by adding this codes and its working fine now . thank you for sharing this. :
$agent = $_SERVER['HTTP_USER_AGENT'];
echo $agent.'<br />';
echo '<h1>Test Suite</h1>';
$parsedagent = parse_user_agent($agent);
echo "<pre>";
print_r($parsedagent);
echo "</pre>";
Comment by: dac on
Comment by: ~BoogL~ on
Mozilla/5.0 (Linux; Android 4.0.4; GT-P5100 Build/IMM76D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36
User Agent Returned: Chrome version 537.36 on Android but not version 27.0.1453.90
Opera/9.80 (J2ME/MIDP; Opera Mini/4.4.28000/30.3061; U; ru) Presto/2.8.119 Version/11.10
User Agent Returned: Opera version 11.10 but not Opera Mini 4.4.28000
And i'm not shure that Mozilla/5.0 (Linux; U; Android 4.1.1; ru-ru; A701 Build/JRO03H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30
is Chrome version 4.0
Comment by: Jesse G. Donat on
Comment by: Mark on
Here is a problem... it returns Android Version 1.0
Comment by: Kalla on
Comment by: Varuzhan Kankanyan on
Hello, I think will be reasonable to add an option to return also device type ( Desktop, Mobile, Console). Bellow the code to be added:
$platforms = array(
'desktop' => array('Windows','Linux','Macintosh','Chrome OS'),
'mobile' => array('Android','iPhone','iPad','Windows Phone OS','Kindle','Kindle Fire','BlackBerry','Playbook'),
'console' => array('Nintendo 3DS','Nintendo Wii','Nintendo WiiU','PlayStation 3','PlayStation Vita','Xbox 360')
);
foreach($platforms as $k=>$v){
foreach($v as $pl){if($pl == $platform){$device = $k;break;}
}
}
return array( 'device' => $device, 'platform' => $platform, 'browser' => $browser, 'version' => $version );
Comment by: Jesse G. Donat on
My goal is to keep this more of a strict parser and not an interpreter.
I really want to avoid feature creep.
Comment by: Ueli on
Just a very minor suggestion for the documentation. I wasn't sure if the currently detected items are effectively return values, or if they are just listed in a descriptive style. So I went into the code to be sure. This would be more obvious if the items were enclosed by quotes, for a list like
o Mobile
- 'Android'
- 'Windows Phone OS'
- 'Kindle Fire'
Comment by: Const on
The string is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.89 Vivaldi/1.0.83.38 Safari/537.36 and is identified as Chrome 40.
Comment by: Ben on
Comment by: Cli on
Comment by: Jesse G. Donat on
See: https://travis-ci.org/donatj/PhpUserAgent
If you had left an actual email address I'd have been happy to help you resolve your issues. Alas.
Comment by: Martin on
Really appreciated. Keep up the good work.
Comment by: Philippe on
Just one thing, it looks like Opera Neon is not detected correctly
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.21 Safari/537.36 MMS/1.0.2531.0
Comment by: Jesse G. Donat on
While I can update the code to sniff it out since it doesn't indicate what it actually is, it's not a mainstream enough browser to justify the additional code. Worldwide usage is basically nil.
It's in their own words a "Concept Browser". If and when it becomes anything more than a concept, I will gladly support it, but until then…
Comment by: Gonzo Far on
Why am I getting these errors?
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\dev\UserAgentParser.php on line 23
Parse error: syntax error, unexpected ':' in C:\dev\UserAgentParser.php on line 73
Comment by: Jesse G Donat on
My library supports all the way back to PHP 5.3.0 which is 11 years old. You're running older something older than that.
It's probably time to upgrade.
Comment by: Aishwarya Easwar on
1. Chrome 26.0.1410.43, 31.0.1650.57, 27.0.1453.116, 16.0.912.63, 23.0.1271.97, 22.0.1229.79, 28.0.1500.71, 5.0.375.53, 11.0.696.68, 14.0.835.187, 10.0.648.133, 12.0.742.112
2. MSIE 7.0, 6.0, 8.0
3. Mobile Safari - older versions
4. Firefox 22, 31, 23, 3.6.11, 3.6.7, 5, 4.0.1, 30, 9.0.1, 32, 27, 2.0.0.10
5. Mobile_Chrome 28.0.1500.94, R83-13020.67.0, R84-13099.110.0, R85-13310.59.0
6. Mobile Safari - older versions
Comment by: Jesse Donat on
Comment by: Scott on
use donatj\UserAgent\UserAgentParser;