Hello everyone, I know little about php, so I ask. I found this function here:

function getInfoBrowser(){ $agent = $_SERVER['HTTP_USER_AGENT']; preg_match("/(MSIE|Opera|Firefox|Chrome|Safari|Chromium|Version)(?:\/| )([0-9.]+)/", $agent, $bInfo); $browserInfo = array(); $browserInfo['name'] = ($bInfo[1]=="Version") ? "Safari" : $bInfo[1]; $browserInfo['version'] = $bInfo[2]; return $browserInfo;} 

pop it up like this: $browser = getInfoBrowser(); var_dump($broswer); $browser = getInfoBrowser(); var_dump($broswer); everything went fine

but I need to redirect users of old browsers (Chrome lt 8.0, Safari lt 5.0, Firefox lt 4.0, MS IE lt 9.0, Opera lt 8.2) to the OldBroswer.php page, provided that for example the value of ['name'] = 'Chrome' and [ 'version'] = <8.0, how to implement it?

  • So everything is already in the data ... $ browserInfo ['name'] - browser type, $ browserInfo ['version'] - version. For comparison, you need to do a version conversion into an integer and can make an integer comparison. - Daniel Protopopov
  • @DanielProtopopov I meant something like this (if ($ browser ['name'] === 'Chrome' || $ browser ['version'] === <8.0) - Eugen Eray
  • So what's the matter, check and redirect through header () - php.net/manual/ru/function.header.php - Daniel Protopopov
  • And if version 47.0.2562.1? When you write floatval ($ browser ['version']), it gives 47 - Eugen Eray
  • one
    @EugenEray, do you really have users with such ancient browsers? Besides IE? Probably only Krivorukov spammers in bots forgot user agents to change :) - Visman

2 answers 2

The version may take the form, for example, 56.0.2924.87 and you will not translate to float. As an option to implement a function that compares 2 lines, for example, true - the version is greater than the compared value, false - on the contrary, -1 - equal:

 function compareVersion($version, $compare) { $versionArr = explode('.',$version); $compareArr = explode('.',$compare); $len = count($versionArr) - count($compareArr); if($len > 0) { $compareArr = array_pad($compareArr,count($versionArr), 0 ); } if($len < 0) { $versionArr = array_pad($versionArr,count($compareArr), 0 ); } for($i = 0; $i < count($versionArr); $i++) { if((int)@$versionArr[$i] > (int)@$compareArr[$i]) { return true; } else if((int)@$versionArr[$i] < (int)@$compareArr[$i]) { return false; } } return -1; } compareVersion($browserInfo['version'], '55.0.29955'); 
  • @ Ipatiev, yes, thank you, did not know about such a function. I myself had such a crutch implemented. - Kostiantyn Okhotnyk

In principle, I thought that this was also the norm, since I do not need a super accurate detection system.

$browserInfo = getInfoBrowser(); $roughVersion = floatval($browserInfo['version']); $name = $browserInfo['name']; if(stristr($name, "Chrome") && $roughVersion <= 8){ header("Location: OldBrowser");} else if (stristr($name, "Firefox") && $roughVersion <= 4){ header("Location: OldBrowser");} else if (stristr($name, "Safari") && $roughVersion <= 5){ header("Location: OldBrowser");} else if (stristr($name, "MSIE") && $roughVersion <= 9){ header("Location: OldBrowser");} else if (stristr($name, "Opera") && $roughVersion <= 9){ header("Location: OldBrowser");}

  • Better one big multiline if and single call header - jekaby
  • @jekaby Yes, thanks, I'll change it now. - Eugen Eray