Detect if user uses newest version of Firefox?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

All the example that should be needed is posted in the thread I linked to.
neoaddict
Forum Commoner
Posts: 44
Joined: Thu Jan 12, 2006 12:28 pm

Post by neoaddict »

This is my current code:

Code: Select all

<?php
$ua = $_SERVER['HTTP_USER_AGENT'];

function feyd_get_browser($user_agent = null, $return_array = false) {

        $ini = 'php_browscap.ini';
        $default = 'Unknown';
       
        if ($user_agent === null)
        {
                if(isset($_SERVER['HTTP_USER_AGENT']))
                {
                        $user_agent = $_SERVER['HTTP_USER_AGENT'];
                }
                else
                {
                        $user_agent = $default;
                }
        }

        $data = parse_ini_file($ini,true);

        $final = array();
        $found = false;

        foreach($data as $pattern => $info)
        {
                $name_regex     = '^' . str_replace(array('?','*'),array('.','.*'),strtolower($pattern)) . '$';
                $regex          = '#^' . str_replace(array('\\?','\\*','#'),array('.','.*?','\\#'),preg_quote($pattern)) . '$#i';
                if (preg_match($regex,$user_agent))
                {
                        $found = true;
                        $final['browser_name_pattern'] = $pattern;
                        $final['browser_name_regex']   = $name_regex;
                        if (isset($info['parent']) and isset($data[$info['parent']]))
                        {
                                $final = array_merge($final,$data[$info['parent']],$info);
                        }
                        else
                        {
                                $final = array_merge($final,$info);
                        }
                        break;
                }
        }
       
        if(!$found) {
                $final = $data[$default];
        }
       
        if ($return_array)
        {
                return $final;
        }
        else
        {
                return (object)$final;
        }

if (($final[version] >= "1.5.0.3") && ($final[browser] == "Firefox") || ($final[browser] == "Minefield") || ($final[browser] == "BonEcho")){
echo "You're using the most updated version of Firefox/Minefield/BonEcho!";
}
else{
echo "Please upgrade your browser.";
}
}

feyd_get_browser($user_agent = null, $return_array = false);
?>
It's not giving me any output. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The code you've added will never be run because the function terminates in the lines immediately before it. Use the return value from the function to create your check.
neoaddict
Forum Commoner
Posts: 44
Joined: Thu Jan 12, 2006 12:28 pm

Post by neoaddict »

I think I've got it working. :D

Code: Select all

<?php
$ua = $_SERVER['HTTP_USER_AGENT'];

function feyd_get_browser($user_agent = null, $return_array = false) {

        $ini = 'php_browscap.ini';
        $default = 'Unknown';
       
        if ($user_agent === null)
        {
                if(isset($_SERVER['HTTP_USER_AGENT']))
                {
                        $user_agent = $_SERVER['HTTP_USER_AGENT'];
                }
                else
                {
                        $user_agent = $default;
                }
        }

        $data = parse_ini_file($ini,true);

        $final = array();
        $found = false;

        foreach($data as $pattern => $info)
        {
                $name_regex     = '^' . str_replace(array('?','*'),array('.','.*'),strtolower($pattern)) . '$';
                $regex          = '#^' . str_replace(array('\\?','\\*','#'),array('.','.*?','\\#'),preg_quote($pattern)) . '$#i';
                if (preg_match($regex,$user_agent))
                {
                        $found = true;
                        $final['browser_name_pattern'] = $pattern;
                        $final['browser_name_regex']   = $name_regex;
                        if (isset($info['parent']) and isset($data[$info['parent']]))
                        {
                                $final = array_merge($final,$data[$info['parent']],$info);
                        }
                        else
                        {
                                $final = array_merge($final,$info);
                        }
                        break;
                }
        }
       
        if(!$found) {
                $final = $data[$default];
        }
       
        if ($return_array)
        {
                return $final;
        }
        else
        {
                return (object)$final;
        }
}

if (($final['browser'] == "Firefox") || (stristr($ua, "BonEcho/2.0")) || (stristr($ua, "Minefield/3.0")) && ($final['version'] >= "1.5.0.3") && (!stristr($ua, "Opera"))){
echo "You're using the most updated version of Firefox/Minefield/BonEcho!";
}
else{
echo "Please upgrade your browser.";
}
?>
Post Reply