Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

Check if Desktop Computer is viewing mobile site and redirect

In PHP how can I check if a desktop browser is viewing my mobile site and redirect to the desktop site?

I have a microsite that's mobile only and if a desktop hits it I want to redirect to the main company home page.

I tried this:
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
if(strpos($useragent,'Windows') == true || strpos($useragent,'Mac') == true)
{
    header( 'Location: http://you.tc' ) ;
}
?>

Open in new window


But on iphone it's redirecting to the home site and should stay on mobile.  I'm assuming there is something else in the user agent I need to check.
ASKER CERTIFIED SOLUTION
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nathan Riley

ASKER

Changed the logic to the reverse, but worked, thanks.
There's a different way of thinking about this problem today.  We use Twitter Bootstrap instead of building separate sites and redirecting.  You might want to look it up.  The design paradigm is "mobile first" and Bootstrap provides for this, giving you a fluid and adaptable viewport that scales nicely to different devices.  Just a thought...
Agreed, but client wanted it to not be visible by desktops, and if so then redirected to their company homepage.
Uhh... Why not help the client understand the current technology.  Twitter Bootstrap is not exactly new.  It's not a model-T ford, but it's completely mainstream and makes it possible for a single web application to serve sites to many different browser/viewport combinations.  In other words, it is the framework for many a company home page!
It's a microsite for mobile only for a mobile convention, no need for desktop version.