0

我不知道我做错了什么,但脚本不会检查正确的浏览器。

<?
    class rbc_BrowserInfo {

        public $browser = "";
        public $os = "";
        public $lang = "";
        public $mobile = 0;

        public function getBrowserInfo() {
            if (preg_match('/MSIE/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'ie';
            if (preg_match('/Internet Explorer/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'ie';
            if (preg_match('/Firefox/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'firefox';
            if (preg_match('/Mozilla/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'firefox';
            if (preg_match('/Opera/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'opera';
            if (preg_match('/Safari/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'safari';
            if (preg_match('/Chrome/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'chrome';
            if (preg_match('/Camino/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'camino';
            if (preg_match('/Konqueror/i',$_SERVER['HTTP_USER_AGENT'])) $this->browser = 'konqueror';


            if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT'])) || (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
                $this->mobile = 1;
            }


            $os_arr = array ( 'windows' => '(Windows)|(Win)', 'linux'=>'(linux)|(X11)', 'mac'=>'(Mac_PowerPC)|(Macintosh)|(Mac)');
            $this->os = 'other';
            foreach($os_arr as $os=>$ospattern) {
                if (eregi($ospattern, $_SERVER['HTTP_USER_AGENT']))
                $this->os = $os; 
            }

            $this->lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        }
    }

    $browserinfo = new rbc_BrowserInfo();
    $browserinfo -> getBrowserInfo();

    /* EXAMPLES */
    if ($browserinfo->lang!="de") {
        header("Location: index.php?language=de"); /* Browser umleiten falls Sprache nicht Englisch ist */
        exit;
    }

    // or
    if ($browserinfo->mobile==1) {
        header("Location: http://mobile.example.com/"); /* Browser umleiten */
        exit;
    }

    /* // or
    if ($browserinfo->browser=="ie" ){
        echo 'Du surfst mit Safari';
    }
    else {
        echo 'Du surfst nicht mit Safari';
    } */

    if ($browserinfo->browser=="ie") {
    echo '<div class="error">Dude..Get Firefox</div>';
}
?>

如果我将部分更改为

    if ($browserinfo->browser!="ie" ){

比所有浏览器都收到消息..但是如果代码返回

    if ($browserinfo->browser=="ie" ){

甚至比 Internet Explorer 都没有收到通知

4

2 回答 2

0

只是在这里调试,如果您对Firefox的检查执行相同的操作,它是否正常工作?

于 2012-11-19T14:57:08.887 回答
0

使用 MSIE 而不是 IE,我相信应该可以。它在我所有的网站上都有。

于 2013-05-07T00:59:34.080 回答