Skip to content

Commit

Permalink
Make $mobile_first safer
Browse files Browse the repository at this point in the history
To work around browser issues with unreliable cookie setting speed
Adaptive Images now also checks the user agent string.

If a desktop environment is detected, $mobile_first is automatically
over-ridden to "FALSE". If not Adaptive Images will obey the configured
value.

This is a safety fallback and requires testing before I revert the
default $mobile_first configured value to TRUE.
  • Loading branch information
MattWilcox committed Nov 10, 2011
1 parent e28628b commit 16fec3e
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions adaptive-images.php
Expand Up @@ -72,33 +72,21 @@ function findSharp($intOrig, $intFinal) {
/* Browser engine detect
NOTE: only required to work around a bug where some browsers can't set the cookie fast enough on the first visit to the
website. Such browsers therefor act as though no cookie was set on the very first visit. This means we can't
allow them to have $mobile_first set to TRUE, or else they get the mobile version on the first load of the site */
allow desktop browsers to have $mobile_first = TRUE (which we don't want anyway) */
function browser_detect(){
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);

// Identify the browser engine. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari.
if (preg_match('/opera/', $userAgent)) {
$name = 'opera'; }
elseif (preg_match('/webkit/', $userAgent)) {
$name = 'webkit'; }
elseif (preg_match('/msie/', $userAgent)) {
$name = 'msie'; }
elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) {
$name = 'gecko'; }
else { $name = 'unrecognized'; }

// What version?
if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches)) {
$version = $matches[1]; }
else {
$version = 'unknown'; }

return $name;
// Identify the OS platform. Match only desktop OSs
if (
preg_match('/Macintosh/', $userAgent) ||
preg_match('/Windows NT/', $userAgent)
) {
return TRUE;
}
}

/* Do we need to switch mobile first off? */
if(browser_detect() == "msie" ||
browser_detect() == "gecko") {
if(browser_detect()){
$mobile_first = FALSE;
}

Expand Down

0 comments on commit 16fec3e

Please sign in to comment.