I’ve pushed out v1.4.0 of ua-parser-php. ua-parser-php is the PHP library for the official ua-parser project. Why use ua-parser? If you need a simple library to slice & dice user agent strings into understandable components (e.g. browser, OS, device) then it’s the project for you. ua-parser-php goes a step further by also attempting to categorize browsers as mobile, tablet, computer, or spider.
As the ua-parser team updates the YAML file and its regular expressions you will want to update your local copy of regexes.yaml. The easiest way to do this on a *nix system is to use cron. I’ve added some simple flags, -silent and -nobackup, to make it easier to set-up an intelligent cron job. -silent is very important and I highly encourage you to use the flag if you use ua-parser-php with cron. By default, ua-parser-php writes out process updates to the command line as it fetches the regexes.yaml file. When using cron this will end up as an email and will either fill the spool on the machine or your inbox.
So here are some examples of commands you can use with cron:
// any of the following can be used as the basis for your cron job.// you shouldn't have to grab the regexes.yaml file more than once a day.// the following examples will run at midnight each day
// update your regexes.yaml file with the following0 0 * * * php /path/to/UAParser.php -get
// use the -silent flag to turn off status updates when grabbing latest regexes.yaml 0 0 * * * php /path/to/UAParser.php -get -silent
// use the -nobackups flag to avoid creating back-ups when grabbing latest regexes.yaml 0 0 * * * php /path/to/UAParser.php -get -nobackup
// mix both flags0 0 * * * php /path/to/UAParser.php -get -silent -nobackupBecause the PHP version of ua-parser supports extra browser categorizations like isMobile you can use ua-parser-php as the basis for a simple redirect script. The following should show just how easy it can be:
<?php
// require the ua-parser-php library require_once("/path/to/UAParser.php");
// parse the requesting user agent $result = UA::parse();
// redirect phones, to redirect tablets use isMobileDevice if ($result->isMobile) { header("location:http://dmolsen.com"); }
// run through the rest of your code for the page for desktop devices & spiders
?>Obviously, the big feature of ua-parser is matching user-agent strings and giving you usable data. The following user-agents have been added and tested:
Have you tried out ua-parser-php and found an incorrect match? Drop a note in the ua-parser issue list on GitHub.
Editor’s note: I have no idea if ‘cron’ can be used as a verb but, hey, what the hell.