Search code examples
phparraysbrowscap

How to build array for checking user agent against browscap and browser version variable and output a if statement?


I have a file that reads the users browser agent and if it matches a browser name and version it outputs a if statement.

$firefoxv = "66.0";
$chromev = "73.0";

if ($browser1['browser'] == "Firefox") {
    if ($browser1['version'] !== $firefoxv)  { 
            echo '<p style="margin-top: 20px;"><b class="label label-danger"><i class="fa fa-exclamation"></i> Your Browser is Out of Date</b></p>Current Version: <b class="label label-success">'.$firefoxv.'</b> - Your Version: <b class="label label-danger">'.$browser1['version'].'</b>';
        } else {
            echo '<p style="margin-top: 20px;"><b class="label label-success"><i class="fa fa-check"></i> Your Browser is Up to Date</b></p>';
        }
}

if ($browser1['browser'] == "Chrome") {
    if ($browser1['version'] !== $chromev)  { 
            echo '<p style="margin-top: 20px;"><b class="label label-danger"><i class="fa fa-exclamation"></i> Your Browser is Out of Date</b></p>Current Version: <b class="label label-success">'.$chromev.'</b> - Your Version: <b class="label label-danger">'.$browser1['version'].'</b>';
        } else {
            echo '<p style="margin-top: 20px;"><b class="label label-success"><i class="fa fa-check"></i> Your Browser is Up to Date</b></p>';
        }
}

Now I am adding a few more to this list and I want to be able to have all of them in 1 statement so I'm not copying the same code over and over again. My new variables will be listed like this:

$android_browser="Android";
$android_ver="5.1";
$android_webView_browser="Android WebView";
$android_webView_ver="33.0";
$chrome_browser="Chrome";
$chrome_ver="73.0";
$chromium_browser="Chromium";
$chromium_ver="73.0";

I want it to have the same if statement as above but work with the new set of variables. Can this be done with some kind of an array or something similar?

It needs to basically look and match the browser name and then once it matches check the version and if users browser matches version in the variable to display "Browser Up to Date" otherwise display "Browser Out of Date".

Thank You


Solution

  • I would create an Array where you can directly access browser version information using the value of what you're calling $browser1['browser'].

    For example:

    $versions = Array(
      "Firefox" => "66.0",
      "Chrome" => "73.0",
      ...
    );
    

    This way, you can quickly get the latest version of the browser by referencing the value of $versions[$browser1['browser']]

    To that effect, you can check and see if that value is NULL from an unknown browser type.

    From there, all you have to do to check any browser/version scenario is a single statement:

    if ($browser1['version'] < $version[$browser1['browser']]){
        echo "<p style=\"margin-top: 20px;\"><b class=\"label label-danger\"><i class=\"fa fa-exclamation\"></i> Your Browser is Out of Date</b></p>Current Version: <b class=\"label label-success\">{$version[$browser1['browser']]}</b> - Your Version: <b class=\"label label-danger\">{$browser1['version']}</b>";
    }
    else {
        echo "<p style=\"margin-top: 20px;\"><b class=\"label label-success\"><i class=\"fa fa-check\"></i> Your Browser is Up to Date</b></p>";
    }
    

    To add new browsers or versions, all you have to do is update the Array. There's more you'll want to do to make sure you're getting actual values back from $versions, but that should be enough to get you started without having to repeat any statements.