I've got a very strange issue that I've never seen in WordPress. I've just converted a big site over to a new responsive theme. The site has about 1,400 articles and there are ads on it. The way we are displaying the ads is through conditional logic which looks at the $_SERVER["HTTP_USER_AGENT"] variable and checks to see if it's desktop or mobile. The ads are only supposed to show on desktop so if it's mobile, it just doesn't echo is out. I've used the code from the answer here: https://stackoverflow.com/questions/4117555/simplest-way-to-detect-a-mobile-device, though I know there is a native WordPress function wp_is_mobile(), but it's based on the same idea.

It works fine on most posts but there are a bunch of posts that it doesn't work on (i.e. the ad shows on mobile). I could not figure it out and ended up thinking that maybe the $_SERVER["HTTP_USER_AGENT"] variable wasn't being set on those pages for some reason. I used 'print_r($_SERVER)' to inspect and as soon as I loaded the page with those variables printed, the ad disappeared. Every subsequent page visit after that on that specific post does not show the ad. It's as if the SERVER variables weren't recognized until I printed them.

Another thing is that when it was showing the ad on mobile and then I'd log into WordPress on mobile and go to that post, it wouldn't show (which is what I want). So the only time it was showing was on some of the posts only when logged out of WordPress.

I am using W3 Total Cache and I don't know if that would affect server variables but it should be noted since I know that plugin can cause little things here and there.

Any ideas on why this would be happening?

  • 1
    well if the pages are cached then there's your answer.
    – Milo
    Aug 5, 2014 at 1:21
  • @Milo, but why would that prevent $_SERVER variables from being read? It's as if there are no server variables at all on those posts... Aug 5, 2014 at 23:35
  • php isn't executed when the pages are served from cache, you're seeing a cached page that was primed from a non-mobile request. cache plugins typically serve live pages when you're logged in, hence it would appear to function correctly once you've logged in. you can easily test all of this by disabling the cache plugin and seeing how it behaves.
    – Milo
    Aug 5, 2014 at 23:43
  • @Milo, right. That makes total sense. So then the issue is, can server-side browser detection and W3 Total Cache be used successfully together then? I suppose it's a matter of just resetting the cache entirely? Aug 5, 2014 at 23:49
  • you can't use full page caching, but search around for "fragment cache" with W3 total cache, it'll let you keep specific elements live while caching the rest of the page.
    – Milo
    Aug 5, 2014 at 23:53

1 Answer 1


Turn off your cache, and the variables should work as expected.

Caching saves copies of your output and doesn't execute the code every time the page loads. As a result, server variables might contain information form a past page load as opposed to a current page load.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.