移动检测和cURL重定向给出404

时间:2015-03-23 19:37:56

标签: php curl mobile

所以我有一个桌面(www.example.com/friends.php)和单独的移动网站(m.example.com/friends.php)。并非移动网站上存在桌面网站上的所有页面。

目标:我正在检测移动设备,如果证明是真的,我想在.com(friends.php)之后捕获桌面网址,并替换移动网域&# 34; m.example.com + $ pagename"。然后我想通过cURL检测移动域上是否存在该页面(见下文)。如果它存在,我希望它重定向到相应的移动页面,如果它不存在(返回404),它应该重定向到基本移动域。

我目前将功能设置为在基于404找到移动页面时发出警告。

问题:当我测试一个我知道的页面时,我知道它不存在,它会警告"找到移动页面"总是,然后将重定向到移动网站404页面。我在这里错过了什么?我已经盯着这几个小时了。

守则:

function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);}

if(isMobile()){

    function curPageName() {
         return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
        }

    $pagename = curPageName();      

    $mobileurl = 'http://m.example.com/' + $pagename;
    $curl = curl_init($mobileurl);
    curl_setopt($curl, CURLOPT_NOBODY, true);

    $result = curl_exec($curl);
    if ($result !== false) {
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  
        if ($statusCode == 404) {
            echo "<script type='text/javascript'>alert('mobile url not found');</script>";
            header("Location: http://m.example.com/");
        } else {
            echo "<script type='text/javascript'>alert('mobile url found');</script>";
            header("Location:" + $mobileurl);}
    } else {
        echo "<script type='text/javascript'>alert('mobile url not found');</script>";
        header("Location: http://m.example.com/");
    }

} else {
return false;

}

0 个答案:

没有答案
相关问题