Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

DetectMobile

Summary: Determines whether the browser is a mobile device.
Version: 31-Dec-2008 (2.0.1)
Prerequisites: None
Status: Active
Maintainer: DaveG
Categories: Mobile, PDA, Cookbook
License: SHARED SOURCE LICENSE

Description

Provides a means of detecting whether the user browsing a wiki is using a mobile device. Can be used within PmWiki (or PHP in general) to either load mobile specific skins, or perform mobile related actions.

Download and Installation

  1. Download the script. (detect_mobile.zipΔ)
Warning: the script contains some long lines, so if you open it in a text editor, be sure not to save the files with word-wrap on.
  1. Unzip it into your cookbook directory (usually pmwiki/cookbook/).
  2. Add the following to your local configuration file:
include_once("$FarmD/cookbook/detect_mobile.php");

Using the Script

Simply call the function from within PHP:

detect_mobile_device();

For example, you might use it in config.php to use a different skin if a mobile device is detected.

if(detect_mobile_device()) {
   $Skin = 'skidoo/PDA';
} else {
   $Skin = 'skidoo';
}

Optional Parameters

The function has five parameters that can be passed to it which define the way it handles different scenarios. These parameters are:

  • iPhone (default: true) - Set to true to treat iPhones as mobiles, false to treat them like full browsers or set a URL (including http://) to redirect iPhones and iPods to.
  • Android (default: true) - Set to true to treat Android handsets as mobiles, false to treat them like full browsers or set a URL (including http://) to redirect Android and Google mobile users to.
  • Opera Mini (default: true) - Set to true to treat Opera Mini like a mobile, false to treat it like full browser or set a URL (including http://) to redirect Opera Mini users to.
  • Mobile Redirect URL (default: false) - This should be full web address (including http://) of the site (or page) you want to send mobile visitors to. Leaving this blank will make the script return true when it detects a mobile.
  • Desktop Redirect URL (default: false) - This should be full web address (including http://) of the site (or page) you want to send non-mobile visitors to. Leaving this blank will make the script return false when it fails to detect a mobile.

Known Issues

  • None so far...

Change Log

31-Dec-2008 (2.0.1)

Contributors: DaveG

  • new: Added sunrise to piped list.
  • chg: Altered structure of script; removed long-lines.
  • chg: Replaced use of eregi with preg_match (binary safety).
  • bug: Added check for PmWiki at header.

30-Dec-2008 (2.0.0)

Contributors: DaveG

  • Recognizes newer mobile devices.
  • Accepts 5 parameters to determine what to do with specific mobile devices that are capable of displaying full-version web pages.

7-Oct-2007 (1.0.0)

Contributors: DaveG

  • First release.

Credit

  • Mobile device detection is performed by the PHP script available from Andy Moore.

Comments

I'd love to know if you use this recipe -- edit this page and leave a comment below!


28-December-2008: (Ian MacGregor) I installed this script as per your instructions above and was presented with this error:

Parse error: syntax error, unexpected T_IF in public/cookbook/detect_mobile.php on line 119
  • 28-Dec-2008: Sounds very odd -- detect_mobile.php should only have 54 lines. Some of the lines are long -- did you open the file in a text editor that might have auto-wrapped them? DaveG
  • 28-Dec-2008: Yes, I did open it in a text editor, which I do with all recipes just to check the code. However, I did not "save" the document so it shouldn't have been changed at all.
UPDATE: 20081230 - Dave, I tried it again using only command line tools to download, unpack, and setup the local config.php file and it's no longer yielding any errors. However, the recipe works fine on my website from a desktop computer, but doesn't switch skins when viewed on my mobile phone. I think the reason for this is that the mobile phone I have, Sidekick LX, uses the following user agent string:
Mozilla/5.0 (Danger hiptop 2.0; U; AvantGo 3.2)
I think the recipe is seeing that user agent string as 'mozi' which is also used in the Firefox web browser for desktop environments, thus the skin switch isn't happening. Is there a way to detect the "AvantGo" or "hiptop" bits of the above user agent string? I think that would help a great deal for Sidekick, Sidekick Color, Sidekick2, Sidekick3, Sidekick Slide, Sidekick LX, and Sidekick08 users. I'm planning to switch phones in the next few months to the T-Mobile G1 and the user agent string for that phone is:
Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2
But, being a Linux user myself, the best way to detect that user agent string is to detect either "Android" or "Mobile" as anything else will likely see browsers used under the desktop Linux OS as mobile when they aren't. Ian MacGregor
  • 30-Dec-2008: I'll take a look tonight. I haven't updated the script for a while, so there's probably a few new browser types to add. DaveG
  • 30-Dec-2008: I've upgraded the code. I can't perform an actual test, but the upgrade should accommodate the agent strings you've listed above. Let me know how it goes. Enjoy that G1 :) DaveG
Edit - History - Print - Recent Changes - Search
Page last modified on December 31, 2008, at 11:15 AM