Kamil Sindi
Kamil Sindi

Reputation: 22822

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?

Upvotes: 1

Views: 2354

Answers (3)

OKEAGU GODWIN
OKEAGU GODWIN

Reputation: 11

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

Upvotes: 0

Kim
Kim

Reputation: 1804

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

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318478

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

Upvotes: 3

Related Questions