Archive for June, 2010

How To: PHP – Enable Features for Specific Devices

Here’s another quick example on how to use MobileESP project code to optimize your web site.

Let’s say that you have a single web page optimized for all iPhone Tier devices. This tier of devices includes iPhone, iPod Touch, Android, WebOS, and Windows Phone 7. And on this web page, you want to show a link to the respective app download page for each device. In other words, you only want to show the link to download the iPhone app for iPhone & iPod Touch devices, only show the Android download link to Android devices, and so on. Here’s how you’d do it.

In the page header, include the MobileESP project code:

<head>
<?php
include("/folder/mdetect.php");
//Instantiate the object to do our testing with.
$uagent_obj = new uagent_info();

You may also want to save some variables that you can reuse in multiple places on the page. For example:

//Detect methods return 1 for true, 0 for false
$isIphoneIpod = $uagent_obj->DetectIphoneOrIpod(); //Check for both!
$isAndroid = $uagent_obj->DetectAndroid();
$isWebOS = $uagent_obj->DetectPalmWebOS();

Now, further down the HTML in the Body section, you might use logic like this to show the appropriate mobile app download link:

<?php
//Print the variable part of the URL to the HTML source
if ($isIphoneIpod == 1) {
print( "<p><a href=\"http://www.mysite.com/download/iphone.htm\">" ); }
else if ($isAndroid == 1) {
print( "<p><a href=\"http://www.mysite.com/download/android.htm\">" ); }
else if ($isWebOS == 1) {
print( "<p><a href=\"http://www.mysite.com/download/webos.htm\">" ); }
//Print the remainder of the sentence
print( "Download our app today!</a></p>");
?>

That’s it!

Comments (3)

How To: PHP Redirects

We get a lot of questions about using the MobileESP project code in practice. Here’s one example on how to redirect browser traffic to optimized content.

In this scenario, we put a PHP file in the root of a web site (for example, wwww.yoursite.com/index.php), but it could be any folder or any page, as well. To keep this scenario simple, we redirect all traffic like so:

  1. iPhone Tier devices and iPads: Redirect to www.yoursite.com/i/
  2. Rich CSS devices: Redirect to www.yoursite.com/r/
  3. Generic mobile devices: redirect to www.yoursite.com/m/
  4. Desktop browsers and all others: www.yoursite.com/d/

So this is how we would write the code to achieve these goals within the index.php file. This would be the complete contents of the file:

<?php
include("/folder/mdetect.php");
//Instantiate the object to do our testing with.
$uagent_obj = new uagent_info();
//Detect iPhone Tier and iPads...
if (($uagent_obj->DetectTierIphone() == $uagent_obj->true) ||
($uagent_obj->DetectIpad() == $uagent_obj->true))
   { header('Location: http://www.yoursite.com/i/'); }
//Detect Rich CSS Tier...
else if ($uagent_obj->DetectTierRichCss() == $uagent_obj->true)
   { header('Location: http://www.yoursite.com/r/'); }
//Detect All Other Mobile Devices...
else if ($uagent_obj->DetectTierOtherPhones() == $uagent_obj->true)
   { header('Location: http://www.yoursite.com/m/'); }
//Else it's a regular PC browser -- send to regular desktop site
else
   { header('Location: http://www.yoursite.com/d/'); }
?>

That’s pretty much it! This basic strategy can be used in many ways in addition to redirects.

Comments (22)

MobileESP APIs Published!

Just a quick note that I published the complete list of device detection methods for easier perusal.

One page is focused on the server-side programming languages for PHP, Java and ASP.NET. The other page lists the methods for the JavaScript API — with the heavy caveat that JavaScript support is very poor among mobile devices in general.

Enjoy!

Comments