1

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?
Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120

3 Answers3

3

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).

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Here is a question with some good answers on how to do this: http://stackoverflow.com/questions/1005153/auto-detect-mobile-browser-via-user-agent (also, +1 for your second sentence) – Jasper Nov 18 '11 at 00:23
1

I would do this client-side. There is no need to give extra load on your server for this. It can be done through JavaScript or jQuery (and there are many tutorials for this). For example:

http://detectmobilebrowsers.com/

Auto detect mobile browser (via user-agent?)

Feeding can be done trough the new mobile tags (or the @media) and some nice clean HTML5. For example:

http://www.html5rocks.com/en/mobile/mobifying.html

http://webdesign.about.com/od/mobile/a/detect-mobile-devices.htm

Community
  • 1
  • 1
Kim
  • 1,764
  • 1
  • 11
  • 14
  • 1
    What's the rational behind "There is no need to give extra load on your server for this."? Either way your server is going to have to process the redirect, you might as well try to do it as seemlessly as possible on the server-side so the user doesn't have to download a page just to be redirected to another page. This is more significant for a mobile website since bandwidth will most likely be limited for the user. – Jasper Nov 18 '11 at 00:22
  • Not really, you could use html5 caching with a manifest to keep ones bandwidth in order. Also if you're building funcs client side to detect wherether it's a mobile device, I don't see why you would do this serverside. In jquery/javascript you don't have to download a full page before you get redirected... – Kim Nov 18 '11 at 00:27
  • The bandwidth usage I'm talking about is: "how long does it take for a user to see your website after entering it in their address bar." And the minimal amount of time is achieved if the server sends a redirect header to the browser (this also removes parsing time). I'm not sure what you mean about "building funcs client side" but there are some good pre-coded server-side packages for redirecting mobile users. – Jasper Nov 18 '11 at 00:39
  • @Jasper, you are right. I've might had a confusing moment there. Server side redirection is faster then client side. – Kim Nov 18 '11 at 00:47
0

the simplest method i used to detect mobile devices or otherwise using php

  1. strstr
  2. $_SERVER['HTTP_USER_AGENT']

but I used strcontains on php>8

this is the function i created

<?php
function Ismobile($mobile, $notmobile)
{
$device_Info = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strstr($device_Info, 'mobile'))
{
echo $mobile;
}
else
{
echo $notmobile;
}
}

Ismobile('it is a mobile device', 'this is not a mobile device');
?>

https://shopinson.com/php/how-to-detect-a-mobile-device-using-php/ shows the article i created which explains more about including the php>8 function for detecting mobiles