We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41301
    • 10 Posts
    Hello,
    I am fairly new to ModX and I'm running into some caching issues. I created a snippet to determine if the user is coming from a mobile device or a desktop:


    <?php
    $agent = $_SERVER['HTTP_USER_AGENT'];  
      
    if(preg_match('/iPhone|Android|Blackberry/i', $agent)){  
    	return "You're using a mobile device. <br/>
    	QUERY_STRING: " . $_SERVER['QUERY_STRING'] . "<br/>
    	HTTP_USER_AGENT: " . $agent ."<br/>
    	REQUEST_URI: " . $_SERVER['REQUEST_URI'];  
    }  
    else
    {
    	return "You're NOT using a mobile device.  <br/>
    	QUERY_STRING: " . $_SERVER['QUERY_STRING'] . "<br/>
    	HTTP_USER_AGENT: " . $agent ."<br/>
    	REQUEST_URI: " . $_SERVER['REQUEST_URI'];
    }
    ?>
    


    It works perfectly on the first browser/device I tried, but after that any browser/device I use will display the results of the first one until I clear the site cache.

    For example: If I go to my site using my Razr it will display that I'm using a mobile device, but if I then use chrome on my computer it would also say it is a mobile device. If I clear the cache and go to my computer it will say chrome, but then when I go to my razr it will also say I'm using chrome.

    I tried changing a few settings under "caching" with no luck. Has anyone ran into a similar issue? I'm not sure if it is a simple setting or something in my .htaccess file or what. I tried creating this as a static page I did not see these issues so I know it is not an issue with my host/server.

    Thanks,
    Chris

    This question has been answered by chrisbane. See the first response.

    • You can't cache that unless you make the HTTP_USER_AGENT part of the cache key identifying it explicitly. You could add custom caching to your snippet based on the HTTP_USER_AGENT however.
        • 41301
        • 10 Posts
        Thanks for the quick response

        That's the thing, I don't want the user agent to be cached. Somehow the user agent from the first visit to the site(after clearing site cache) is being stored somewhere and every time another browser hits the site the first browser's user agent is being displayed. I would want this to refresh every time the site is hit.
        • Hello,

          Did you try one of the existing mobile detection extras?

          modMobile - http://modx.com/extras/package/modmobile
          Mobile Detection - http://modx.com/extras/package/mobiledetection

            Patrick | Server Wrangler
            About Me: Website | TweetsMODX Hosting
            • 41301
            • 10 Posts
            Thanks AMD,

            I'll give them a try. It is still odd that the agent isn't refreshing. I wouldn't think that would be a common issue. This hasn't ever happened to be before with other CMSs. I'm sure it's some minor setting that i'm overlooking.
            • I normally do responsive or dedicated mobile sites, so I can't offer any help on that front. Jason would be the best person to answer on that front.
                Patrick | Server Wrangler
                About Me: Website | TweetsMODX Hosting
              • I assume you are calling this Snippet of yours uncached?
                • discuss.answer
                  • 41301
                  • 10 Posts
                  haha, I knew it was going to be something easy. Everything is working now. I just assumed the element would be pulled in uncached and never thought to check for that. Thank so much for the help!



                  ...If anyone is reading this and having the same problem. Here is the difference in the way you'll need to make your calls:
                   
                  [[BrowserCheck]]    <--  Cached Snippet Call.
                  [[!BrowserCheck]]   <-- Uncached Snippet Call
                  

                  more info at the bottom of:
                  http://rtfm.modx.com/display/revolution20/Tag+Syntax
                  [ed. note: opengeek last edited this post 11 years, 7 months ago.]
                    • 41301
                    • 10 Posts
                    Hmm my last comment didn't post.

                    But that is exactly what I was missing. I was making the call as [[BrowserCheck]] instead of [[!BrowserCheck]]. I know it was going to be something easy. Thank you for pointing me in the right direction!