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?