Send the Right Web Content to an iPhone

After you determine whether a visitor to your site is using an iPhone or an iPad, you can hide content from either device as you deliver each page. In the contact.php page used in this example, you can see that we deliver different information to the iPhone with the PHP code in the header:

<?php if ($iPhone == true) {// it’s an iPhone ?>      1
      <h3><a href="tel:+12345678901">Call Mark</a> or     2
          <a href="mailto:mark@jellyrancher.com"> Email Mark</a></h3>                               3
      <?php} else {// it’s not an iPhone ?>           4
      <h3>Call Mark at +1 (234) 567-8901 or           5
          <a href="mailto:mark@jellyrancher.com"> Email Mark</a></h3>                               6
<?php } ?>                                                7

The first line shows an if statement; it checks to see whether the variable $iPhone is true. (The = operator is used to assign a value; == evaluates whether two elements are equal; and === determines whether two elements are exactly alike.)

If $iPhone is true, an H3 section on the page offers two options the user can act on: Call Mark, which initiates a phone call on the iPhone, or Email Mark, which creates an e-mail message.

If the visitor to this page isn’t using an iPhone, the phone call link is removed and only the e-mail option is offered. That way, an iPad or a desktop browser doesn’t display a click-to-call that fails on a device with no phone. We keep the e-mail option for anyone with an Internet connection.

We show one type of content and not another by using an if/else statement. Its format resembles the if statement but adds, at the end, the else clause, which replaces the content in the first block — in this example, Call Mark or Email Mark, with a single option, Email Mark, in the second block if the device is not an iPhone.

Note: This example is focused on the iPhone and iPad; to target other devices specifically, you may need to add additional code.

blog comments powered by Disqus
Advertisement

Inside Dummies.com