PHP脚本在一台服务器上运行,但在另一台服

时间:2012-01-30 18:03:09

标签: php apache

我有一种感觉,因为安装的PHP版本存在差异。未正确执行代码的服务器正在运行PHP版本4.3.9。

以下是不起作用的代码部分。

// Function to find browser name
function getBrowser() { 
    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'Linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'Mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'Windows';
    }

    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Internet Explorer'; 
        $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$u_agent)) 
    { 
        $bname = 'Mozilla Firefox'; 
        $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$u_agent)) 
    { 
        $bname = 'Google Chrome'; 
        $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$u_agent)) 
    { 
        $bname = 'Apple Safari'; 
        $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Opera'; 
        $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
        $bname = 'Netscape'; 
        $ub = "Netscape"; 
    } 
    elseif(preg_match('/Android/i',$u_agent)) 
    { 
        $bname = 'Android'; 
        $ub = "Android"; 
    }
    elseif(preg_match('/iPad/i',$u_agent)) 
    { 
        $bname = 'iPad'; 
        $ub = "iPad"; 
    } 
    elseif(preg_match('/iPhone/i',$u_agent)) 
    { 
        $bname = 'iPhone'; 
        $ub = "iPhone"; 
    } 
    elseif(preg_match('/BlackBerry/i',$u_agent)) 
    { 
        $bname = 'BlackBerry'; 
        $ub = "BlackBerry"; 
    } 
    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }

    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        }
        else {
            $version= $matches['version'][1];
        }
    }
    else {
        $version= $matches['version'][0];
    }

    // check if we have a number
    if ($version==null || $version=="") {$version="?";}

    return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
    );
} 

// now try it
$ua=getBrowser();
// Make browser and variables readable
$browser = $ua['name'] . " v" . $ua['version'];
$user_agent = $ua['userAgent'];

编辑:澄清,这里是输出的错误信息(感谢您的抬头)

警告:编译失败:无法识别的字符(?&lt;在第150行/var/www/vhosts/staging.xxxxxxxxx.com/httpdocs/feedback/feedback-accept.php中的偏移3处

注意:未定义的索引:第155行的/var/www/vhosts/staging.xxxxxxxxx.com/httpdocs/feedback/feedback-accept.php中的浏览器

致命错误:调用未定义的函数:第159行的/var/www/vhosts/staging.xxxxxxxxx.com/httpdocs/feedback/feedback-accept.php中的strripos()

感谢您的帮助,您的信息和建议非常好。

Here is the phpinfo() of the server the script doesn't work on

3 个答案:

答案 0 :(得分:3)

没有看到实际的错误是相当困难的,但我想罪魁祸首是strripos(),这只适用于PHP&gt; 5

更新后修改:我猜错了。顺便说一句,在这里看的时候,我发现了这个:

Fatal error: Call to undefined function: strripos()

有一个类似于你的代码veeery和一个公认的解决方案,你可以参考那个SO问题(如果你觉得它有用,一定要归功于那里的回答者)

答案 1 :(得分:1)

有一个对PHP版本有效的PHP函数。

它被称为“get_browser”,它基本上与你的功能做同样的事情。

http://php.net/manual/en/function.get-browser.php

答案 2 :(得分:0)

检查正则表达式的分隔符集是#而不是默认设置的/

用于preg_match调用

$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';