Php 移动检测和跟踪404

Php 移动检测和跟踪404,php,curl,mobile,Php,Curl,Mobile,所以我有一个桌面(www.example.com/friends.php)和单独的移动站点(m.example.com/friends.php)。并非桌面站点上的所有页面都存在于移动站点上 目标:我正在检测一个移动设备,如果它被证明是真的,我想捕获.com(friends.php)后面的桌面URL,并替换移动域“m.example.com+$pagename”。然后,我想通过cURL检测页面是否存在于移动域中(见下文)。如果它存在,我希望它重定向到相应的移动页面,如果它不存在(返回404),它应

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

目标:我正在检测一个移动设备,如果它被证明是真的,我想捕获.com(friends.php)后面的桌面URL,并替换移动域“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;
函数isMobile(){
返回preg|u match(/(android | avantgo | blackberry | bolt | boost | cricket | docomo | fone | hiptop | mini | mobi | palm | phone | pie | tablet | up.browser | up.link | webos | wos)/i“,$|服务器[“HTTP用户|代理]))
if(isMobile()){
函数curPageName(){
返回substr($服务器[“脚本名称”]、strrpos($服务器[“脚本名称”]、“/”)+1);
}
$pagename=curPageName();
$mobileurl=http://m.example.com/'+$pagename;
$curl=curl_init($mobileurl);
curl_setopt($curl,CURLOPT_NOBODY,true);
$result=curl\u exec($curl);
如果($result!==false){
$statusCode=curl\u getinfo($curl,CURLINFO\u HTTP\u代码);
如果($statusCode==404){
回显“警报('未找到移动url');”;
标题(“位置:http://m.example.com/");
}否则{
回显“警报('找到移动url');”;
标题(“位置:+$mobileurl);}
}否则{
回显“警报('未找到移动url');”;
标题(“位置:http://m.example.com/");
}
}否则{
返回false;

}

您应该使用.htaccess文件,并在文件不存在时重定向到主页。。。您所做的将在服务器上产生不必要的负载。也。。。响应性网站设计是一个伟大的解决方案!另外,脚本的问题很简单,在输出头之前不能输出任何内容,因此需要删除echo语句@我知道。不幸的是,我所在的公司落后于时代。我正在做;)@cmorrissey我移除了回声,但它仍能正常工作。我的htaccess文件中定义了一个404文档,这就是它不重定向的原因吗?