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!

Owner of the MobileESP project. For the latest source code downloads, visit: https://github.com/ahand/mobileesp

Posted in How To - PHP
3 comments on “How To: PHP – Enable Features for Specific Devices
  1. Bill says:

    this looks great. I’ve tried using the php version and I keep getting errors:

    like this: Call to undefined function DetectIphoneOrIpod()

    My code is quite simple at this point. 🙂

    `require(‘./mdetect.php’);

    $uagent_obj = new uagent_info();

    $isIphoneIpod = DetectIphoneOrIpod();`

    any ideas? thanks

  2. Hi,

    This part:

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

    Needs to read:

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

  3. Anthony Hand says:

    Thanks for the catch, Benedict! I just made the fixes.

Leave a Reply

Your email address will not be published.