Page 2 of 2

Posted: Mon May 22, 2006 10:54 pm
by feyd
All the example that should be needed is posted in the thread I linked to.

Posted: Tue May 23, 2006 1:28 pm
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. :(

Posted: Tue May 23, 2006 2:06 pm
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.

Posted: Tue May 23, 2006 11:03 pm
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.";
}
?>