移动重定向到特定页面

时间:2014-04-10 16:13:10

标签: php redirect mobile detection modx

相信我,我已经浏览了Modx和Stackoverflow社区,并且找到了这个问题的答案,并且似乎不是我的答案。 #39;我在任何地方寻找。

首先,我在最新的Modx Revo CMS上运行,但这个问题可能真的适用于任何CMS,我想重定向到我的移动子域,但是我需要它去一个特定的页面,因此,如果我发布社交媒体等链接,移动设备上的人将自动重定向到该网页的移动设备。根据设备更改模板不是一种选择。

现在有一次,有一个很棒的附加组件叫做Mobile Redirect,后面是脑用手脑网的创建者(很多人都赞不绝口),很简单:

首先,您创建了一个代码段并将其称为Mobile_Redirect或您想要的任何内容

   <?php
    /*     Mobile_redirect
           01/28/11    1:40 pm cet
           01/28/11    6:14 am est
           11/24/09   10:31 am est

           original code: http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers
           usage: call in the top of your template
           [[!Mobile_redirect? &mID=`1`]]
           [[!Mobile_redirect? &url=`http://redirectdomain.com`]]
           [[!Mobile_redirect? &exclude=`iPad`]]

           link back to main page:
           add &views=1 to the link
           ex: <a href="http://yourmaindemonstrationsite.com?views=1">Link Back to Main site</a>
    */

    // set the MODx document ID of the mobile page
    $mid = isset($mID) ? $mID : "";

    // set the url you want to redirect to
    // http://domain.com   or   http://sub.domain.com
    $url = isset($url) ? $url : 'http://m.demonstrationsite.com';

    // add any names you want to exclude
    $exclude = isset($exclude) ? explode(',', $exclude) : array();

    if(isset($_GET['views']) || $_SESSION['views'] =='1'){
        $_SESSION['views'] = 1;
    }else{
        if(isset($_SERVER["HTTP_X_WAP_PROFILE"]) || preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) {
            if($mid =="" && $url !=""){
                header("Location: ".$url."");
            }else{
                $goto = $modx->makeUrl($mid);
                $modx->sendRedirect($goto);
            }
        }
        if(isset($_SERVER["HTTP_USER_AGENT"])){
            $uastring = $_SERVER["HTTP_USER_AGENT"]; 
            if(preg_match("/Creative\ AutoUpdate/i",$_SERVER["HTTP_USER_AGENT"])) { 
                return false;
            }
            $check_for_mobiles = true;
            foreach($exclude as $not_mobil){
                if( stripos($uastring, $not_mobil) !== false ) {
                    $check_for_mobiles = false;
                    break;
                }
            }

            if ( $check_for_mobiles == true ) {
                $mobiles = array("webOS","midp", "j2me", "avantg", "iphone", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "mobi", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");
                foreach($mobiles as $mobil){
                    if( stripos($uastring, $mobil) !== false ){
                        if($mid =="" && $url !=""){
                            header("Location: ".$url."");
                        }else{
                            $goto = $modx->makeUrl($mid);
                            $modx->sendRedirect($goto);
                        }
                        exit;
                    }
                }
            }
        }
    }

接下来,您在页面上方的页面上放置了一个代码段呼叫:

[[!Mobile_redirect? &url=`http://m.demonstrationsite.com/` ]]

但是,令我惊讶的是,我发现如果我在我的主页上打了一个电话而在所有内页上打了另一个电话(见下文),它会像魅力一样重定向!

[[!Mobile_redirect? &url=`http://m.demonstrationsite.com/` &exclude=`iPad`]]

[[!Mobile_redirect? &url=`http://m.demonstrationsite.com/[[~[[*id]]]]` &exclude=`iPad`]]

现在,真正让这一点更加完美的是返回主站点的能力(正如原始片段中所评论的那样),所有人必须做的就是将以下链接添加到他们网站的菜单中:

http://demonstrationsite.com?views=1

好的,如果一切运转良好,问题是什么?好吧,这个附加组件不再由brainhandles.com的人员维护。

  

两年后,我决定停止维护和更新   码。我目前没有使用它,而且还需要它   对所有不断改变景观的设备进行大修。我&#39; d   建议开源DetectMobileBrowser.com。

好的,很酷,所以我继续检测DetectMobileBrowser.com并获取PHP代码:

 <?php
    $useragent=$_SERVER['HTTP_USER_AGENT'];
    if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))
    header('Location: http://demonstrationsite.com/mobile');
    ?>

虽然它有效,但即使我将位置更改为:

,它也不会重定向到特定页面
 http://m.demonstrationsite.com/[[~[[*id]]]]

无论我如何尝试,它都只会重定向到主页,如果您尝试利用社交媒体进行营销,这不是一件好事...您根本无法发送给某人现在可以翻阅一堆文章吗?

所以,我的问题是这个;作为一个前瞻性的设计师,他们想到了大孩子们玩PHP的地方,我似乎无法将两者整合在一起,以便将来,当DetectMobileBrowser的代码更新时,我可以简单地更新Mobile_redirect片段。我希望保留一个由DetectMobileBrowser提供的更新设备列表,但具有原始Brainhandles代码的功能。

对此的任何帮助都会非常,非常,非常感激。哎呀,即使你可以使用detectmobilebrowser点com代码发布一个更新的Mobile_redirect片段,这样我就可以看到逻辑会让我永远欠你的债务,我会立即与整个Modx社区分享!

干杯

1 个答案:

答案 0 :(得分:0)

改变原始片段并不是那么难。尝试重写版本:

<?php
/*     Mobile_redirect
       01/28/11    1:40 pm cet
       01/28/11    6:14 am est
       11/24/09   10:31 am est

       original code: http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers
       usage: call in the top of your template
       [[!Mobile_redirect? &mID=`1`]]
       [[!Mobile_redirect? &url=`http://redirectdomain.com`]]
       [[!Mobile_redirect? &exclude=`iPad`]]

       link back to main page:
       add &views=1 to the link
       ex: <a href="http://yourmaindemonstrationsite.com?views=1">Link Back to Main site</a>
*/

// set the MODx document ID of the mobile page
$mid = isset($mID) ? $mID : "";

// set the url you want to redirect to
// http://domain.com   or   http://sub.domain.com
$url = isset($url) ? $url : 'http://m.demonstrationsite.com';

// add any names you want to exclude
$exclude = isset($exclude) ? explode(',', $exclude) : array();

if(isset($_GET['views']) || $_SESSION['views'] =='1'){
    $_SESSION['views'] = 1;
}else{
    $useragent=$_SERVER['HTTP_USER_AGENT'];
    if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4))) {
        if($mid =="" && $url !=""){
            header("Location: ".$url."");
        }else{
            $goto = $modx->makeUrl($mid);
            $modx->sendRedirect($goto);
        }
    }
}