We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21122
    • 153 Posts
    Hi guys,

    I’m trying to implement a mobile detection script. Its a PHP script, so I’ve popped it into a snippet, and am calling it from a chunk. The snippet includes another PHP file which I’ve uploaded to my server. I’ve tested it out on a static site, and it worked fine, but I seem to be having issues with MODx. It worked once, then stopped working. Not sure if this is down to cahing or something?

    The snippet is as follows:

    <?php
    require_once('mobile_device_detect.php');
    mobile_device_detect(true,false,true,true,true,true,true,'http://m.mysite.co.uk',false);
    ?>


    Is there a problem with this? I’m calling it uncached in my chunk. I’m on Evo 1.0.2 by the way!

    Thanks

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Are you calling the snippet cached or uncached? I would think it would need to be uncached.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 21122
        • 153 Posts
        Hi Susan,

        I’ve tried both, and its uncached at the moment i.e [[!mobiledetect!]]

        Still no dice!
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Is that a typo? [[!mobiledetect!]] should be [!mobiledetect!]
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 21122
            • 153 Posts
            In Evolution? I thought it was double square brackets for a snippet?
              • 21122
              • 153 Posts
              I see you’re right, single brackets for a snippet in Evolution.

              At least my script does something when I alter the call. It makes my site display a blank page!

              I’m sure I’ll get there at some point!
                • 4041
                • 788 Posts
                  xforum
                  http://frsbuilders.net (under construction) forum for evolution
                  • 21122
                  • 153 Posts
                  Hmmm...That looks close to being exactly what I need.

                  I’m not sure If I’ve utilised the script properly though...I’m not sure if it matters, but my mobile site isn’t a MODx site, its just a static site using the jQuery mobile framework. My main site (that I’m trying to divert mobile users from) is a MODX site though.

                  I’ve put the script into a snippet and calling it uncached at the head of my template. When I try loading the site on my iPhone, it redirects to the home page (id = 1) and a message pops up saying that it "couldn’t be opened because too many red’rects are in operation". Or something like that!

                  This is what I’m trying:

                  <?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://yourmainsite.com?views=1">Link Back to Main site</a>
                  */
                  
                  // set the MODx document ID of the mobile page
                  $mid = isset($mID) ? $mID : "1";
                  
                  // set the url you want to redirect to
                  // http://domain.com   or   http://sub.domain.com
                  $url = isset($url) ? $url : 'm.sitename.co.uk/jquery';
                  
                  // 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("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;
                                  }
                              }
                          }
                      }
                  }
                  ?>
                    • 4041
                    • 788 Posts
                    You might try modifying the $url parameter at the top of the snippet to include the http://

                    $url = isset($url) ? $url : ’m.sitename.co.uk/jquery’;

                    $url = isset($url) ? $url : ’http://m.sitename.co.uk/jquery’;
                      xforum
                      http://frsbuilders.net (under construction) forum for evolution
                      • 21122
                      • 153 Posts
                      Makes no difference, Safari still gives me the same error message.

                      I had a 301 re-direct in operation on my main site, but not the mobile site. Is there any reason why the script has to redirect the user to the home page of the main, non-mobile site?

                      Thanks for the help btw!