Skip to content
Advertisement

Standard Practices in Detecting Mobile Devices and Feeding Pages in PHP and jQuery

I see that mobile versions of websites often begin with an “m.” (e.g. http://m.accuweather.com). I’d like to redirect my mobile users to http://m.mysite.com so that I can display a different page.

  1. What’s the standard practice way to feed mobile devices the mobile version of a site? Does one detect the type of device on the server-side (if that’s even possible) or on the client-side?
  2. How does one detect whether a mobile device is accessing a site on both the server-side (using PHP) and client-side (using jQuery)?
  3. Is it possible to detect if it’s a mobile device on an Apache level? Or would I constantly have to use PHP scripts to check if it’s a mobile device and redirect to the appropriate version of the page?

Advertisement

Answer

The easiest way is checking the user agent, available via $_SERVER['HTTP_USER_AGENT']. While clients can send any string, people usually do not pretend to be a mobile browser if they aren’t (and if they do so – not your problem).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement