We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6491
    • 70 Posts
    Hi, I created a snippet that’s intended to sniff for mobile devices and redirect to the mobile page(s). I got it to work ONCE on my iPhone, but tried it again and it doesn’t work. (maybe I hallucinated)

    The snippet is supposed to load before anything else, so I have it as the first thing, even before the <html> tag.

    Any way here’s the snippet, can anyone tell me what is wrong? ...
    
    <?php
    /**
     * Mobile Detect
     *
     * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
     * @version    SVN: $Id: Mobile_Detect.php 3 2009-05-21 13:06:28Z vic.stanciu $
     */
    
    class Mobile_Detect {
        
        protected $accept;
        protected $userAgent;
        
        protected $isMobile     = false;
        protected $isAndroid    = null;
        protected $isBlackberry = null;
        protected $isOpera      = null;
        protected $isPalm       = null;
        protected $isWindows    = null;
        protected $isGeneric    = null;
    
        protected $devices = array(
            "android"       => "android",
            "blackberry"    => "blackberry",
            "iphone"        => "(iphone|ipod)",
            "opera"         => "opera mini",
            "palm"          => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
            "windows"       => "windows ce; (iemobile|ppc|smartphone)",
            "generic"       => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)"
        );
    
    
        public function __construct() {
            $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
            $this->accept    = $_SERVER['HTTP_ACCEPT'];
    
            if (isset($_SERVER['HTTP_X_WAP_PROFILE'])|| isset($_SERVER['HTTP_PROFILE'])) {
                $this->isMobile = true;
            } elseif (strpos($this->accept,'text/vnd.wap.wml') > 0 || strpos($accept,'application/vnd.wap.xhtml+xml') > 0) {
                $this->isMobile = true;
            } else {
                foreach ($this->devices as $device => $regexp) {
                    if ($this->isDevice($device)) {
                        $this->isMobile = true;
                    }
                }
            }
        }
    
    
        /**
         * Overloads isAndroid() | isBlackberry() | isOpera() | isPalm() | isWindows() | isGeneric() through isDevice()
         *
         * @param string $name
         * @param array $arguments
         * @return bool
         */
        public function __call($name, $arguments) {
            $device = substr($name, 2);
            if ($name == "is" . ucfirst($device)) {
                return $this->isDevice($device);
            } else {
                trigger_error("Method $name not defined", E_USER_ERROR);
            }
        }
    
    
        /**
         * Returns true if any type of mobile device detected, including special ones
         * @return bool
         */
        public function isMobile() {
            return $this->isMobile;
        }
    
    
        protected function isDevice($device) {
            $var    = "is" . ucfirst($device);
            $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[$device] . "/i", $this->userAgent) : $this->$var;
    
            if ($device != 'generic' && $return == true) {
                $this->isGeneric = false;
            }
    
            return $return;
        }
    }
    $detect = new Mobile_Detect();
    
    if ($detect->isMobile()) { 
        header("Location: /mobile/mobilehome");
    	exit;
    }
    ?>
    
    
      • 19132
      • 199 Posts
      I’m not a coder/developer. I use this snippet:

      <?php
      /* Snippet Title: CustomCSS  */
      
      $output = '';
      $cssBase = $modx->config['site_url'] . 'csn4/css/template.css';   /* or wherever you keep your css files */
      
      /* array of browser agent names and their respective .css filenames */
      
      $browserArray = array(
           'iPhone' => 'mobile.css',
           'SymbianOS/9.1' => 'symbian.css',
           'anotherbrowser' => 'otherbrowser.css',
           'yetanother browser' => 'stillotherbrowser.css'
      );
      
      /* see if we recognize one */
      foreach ($browserArray as $key => $value) {
          if (strpos($_SERVER['HTTP_USER_AGENT'],$key)) {
              $output .= $cssBase . $value;
              break;
          }
      }
      
      /* if we didn't find one, use the default css file */
      if ($output == '') {
          $output = $cssBase . 'template_css.css';
      }
      
      return $output;
      ?>
      


      I call it like this in the head section of my templates:
      <link href="[!CustomCSS!]" rel="stylesheet" type="text/css" />


      Works pretty well for me. I believe it’s from BobRay.
        MySQL: 5.0.45
        PHP: 5.2.6
        Linux 2.6.9-023stab048.6-enterprise #1
        cURL enabled
        PDO enabled
        FFox Apple 3.6.8
        Firebug DIS-abled
        • 27708 MODX Staff
        • 2,502 Posts
        I could be wrong but you could make this a plugin to do before the page is actually loaded (OnWebPagePrerender or OnWebPageInit might be the right place to fire this)
          Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Yeah... you’d definitely want to do this as a Plugin. Snippets execute as the page loads, so your headers are already sent. If you tie that code into a plugin, you can execute it before the page loads and affect the behavior the way you need.
            • 19132
            • 199 Posts
            So could a plugin also detect a mobile phone and send it to a subdomain, or maybe better yet, a specific page?
              MySQL: 5.0.45
              PHP: 5.2.6
              Linux 2.6.9-023stab048.6-enterprise #1
              cURL enabled
              PDO enabled
              FFox Apple 3.6.8
              Firebug DIS-abled
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              Sure, a plugin could do that. I wrote a plugin like that for Evo... looks like someone may have already re-hashed it for Revo:
              http://modxcms.com/forums/index.php?topic=61539.0

              Just remember that a snippet executes in-line, as it is literally encountered while the page loads. All Snippets basically load during one single event: page load (the technical name of that event, I don’t know). Plugin execution can be tied to other events, so you can run your script before the page loads (onBeforePageLoad or some such), or before your template is chosen (onBeforeTemplateLoad or something like that). That gives you a way to conduct some logic before the page load happens, then decide what to do, e.g. choose a different template or redirect to a different page.

              You could do this as a Snippet, but it’d be less efficient: the page loads (including its template and all its assets) and at some point on the page, the Snippet executes and says "whoa, this is a mobile device", then it would send a redirect, and the phone would redirect, effectively wasting the first page load for nothing. Make sense?
                • 3749
                • 24,544 Posts
                Quote from: Peter at Apr 25, 2011, 09:15 PM

                So could a plugin also detect a mobile phone and send it to a subdomain, or maybe better yet, a specific page?
                Definitely.

                To send to a specific page:

                $id = 12;  // This could be in the plugin's properties or pulled from a TV.
                $modx->sendRedirect($modx->makeUrl($id));
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 19132
                  • 199 Posts
                  Make sense?

                  Everett, that made perfect sense. I don’t know why but the diff between snippets and Plugins finally registered in my thick, organic gardening brain.

                  @BobRay: that little piece of code makes me want to write my first plugin. For now I’ll check out what Everett wrote and the Revo hash of it.

                  Thank you to both of you. (There’s another forum, non-technical, where you can thank people and the thanks they accrue is displayed under their signature. Wish this forum had that feature).
                    MySQL: 5.0.45
                    PHP: 5.2.6
                    Linux 2.6.9-023stab048.6-enterprise #1
                    cURL enabled
                    PDO enabled
                    FFox Apple 3.6.8
                    Firebug DIS-abled
                    • 3749
                    • 24,544 Posts
                    Quote from: Peter at Apr 26, 2011, 07:04 AM

                    @BobRay: that little piece of code makes me want to write my first plugin.

                    Just do it. wink


                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 19132
                      • 199 Posts
                      For Evo I’m not seeing something like OnBeforePageLoad.

                      Just doing it...
                        MySQL: 5.0.45
                        PHP: 5.2.6
                        Linux 2.6.9-023stab048.6-enterprise #1
                        cURL enabled
                        PDO enabled
                        FFox Apple 3.6.8
                        Firebug DIS-abled