I have a website with a full browser and jQuery Mobile interface and users can switch between the two. Their preference is stored in a cookie. By default the site serves the full version to visitors whitout a preference cookie, but to make it better for modern smartphone users I want their default to be JQM. So, I have this code in the constructor of my base controller class:
class BaseController
{
protected $mobile; // must be 0 or 1, cookies can't handle boolean 'false'
public function __construct()
{
if (isset($_COOKIE['mobile']))
{
$this->mobile = $_COOKIE['mobile'];
}
else
{
$this->mobile = $this->isSmartphoneWithCookies();
setcookie("mobile", $this->mobile, time() + 7776000, '/', ''); // 90 days
}
}
private function isSmartphoneWithCookies()
{
$SMARTPHONE_WITH_COOKIES = "android.+mobile|blackberry|ip(hone|od)|opera m(ob|in)i";
return preg_match("/$SMARTPHONE_WITH_COOKIES/i", $_SERVER['HTTP_USER_AGENT']) ? 1 : 0;
}
// the rest of my controller class....
}
Here I prefer speed to accuracy, so I am not looking for a slower, up-to-date lookup service as suggested here: Simple Smart Phone detection
Just a quick indication that the visitor has one of the current top smartphone/tablet browsers that support cookies and JQM.
Can someone suggest an improvement for my $SMARTPHONE_WITH_COOKIES? Or point me to a collection of UA signatures that fit my use case? Specifically, is it safe to have blackberry in the list? Am I overlooking a popular capable browser?
By design, the primary UI selector for my app is the user. I do not want to delegate that to a detection script. The bit of detection in question is just to give a clever default. It is a risky feature, because a false positive (for example a smartphone without cookie support) can make my site totally inaccessible to some users. So if it fails, it needs to fail towards the standard interface.
For what it's worth, I considered the following:
So, what I am left with is:
$SMARTPHONE_WITH_COOKIES = "android.+mobile|iphone|ipod";
The cost of keeping it this light and simple is that I will have to reconsider this question again every two years or so. I will also have many mobile visitors that will need to do an extra click to get the mobile version.