Code

Quick Function: Force iPhone redirection

I use one of my scripts to display a mobile version of my sites using the RSS feeds, but sometimes I have to redirect people automatically, so I usually use this piece of code somewhere:


$useragents = array ("iPhone","iPod","blackberry","palm","smartphone","iemobile");
$oniphone = false;
foreach ( $useragents as $useragent ) { if (eregi($useragent,$container)){ $oniphone = true; } }
if($oniphone){header("Location:/m/");exit;}

If I’m on a wordpress site, then the code is placed in my functions.php file for my theme. If I’m on another site, then I usually place it on a file I know will get called.

Generally, you can change it to look for a cookie if you choose to let people opt for the full version of your site, but otherwise, this code comes in handy.

Post Author

This post was written by who has written 384 posts on Roger Stringer.

Roger Stringer is a web developer, writer and editor with over 10 years of experience in web development. Roger Stringer is the founder of sites such as TheInterviewr and DBStract and is the editor of sites such as FoodJumper, Foodizu and Attack Of Code.

One Response to “Quick Function: Force iPhone redirection”

  1. claudio July 27, 2009 at 11:52 pm #

    Thanks for this snippet!

Leave a Reply