I am redesigning a site and I want to have it compatible with the IPad. Could someone show me the code in either JavaScript or PHP so that when someone comes to my site on an IPad they will be automatically redirected to a site that is narrower, contains no Flash graphics and the navigation system does not use mouseover flyouts?

Apple provides this useragent code:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

The code I need is to detect the IPad and then redirect the user to another webpage.
Thanks

Recommended Answers

All 3 Replies

The following construct does the redirect:

if (preg_match( '~iPad;~', $_SERVER['HTTP_REFERER'])) {
  header( "Location: http://$mySpecialIpadSite" );
  exit;
}

Refine the preg expression and substitute your site for $mySpecialIpadSite.

Sorry to have to ask, but I don't understand the refinement to the preg_match function.
I tried this:

<?php
  if (preg_match( '~iPad~;', $_SERVER['HTTP_REFERER'])) {
      header( "Location: http://www.rspsiotedesign.com/test.html" );
      exit;
       }
?
and this:
<?php
  if (preg_match( '\iPad\;', $_SERVER['HTTP_REFERER'])) {
      header( "Location: http://www.mysite.com/test.html" );
      exit;
       }

with no luck. Would appreciate the help. Thanks
?

Learn about preg regular expressions.
Your regular expressions are malformed.
Learn about debugging. Configure your server or your script (with error_reporting(E_ALL) so that it gives you the appropriate error message Warning: preg_match() [function.preg-match]: Unknown modifier ';' in x.php on line y As long as you search for exact strings only, you better use strpos instead of preg_match.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.