0

How can I know if a user has come to my website from a Smart-Phone (especially iPhone and Android)?

I want to redirect the user to a specialized website, so both PHP detection and htaccess detection are good.

More specifically, the user will come from an AdWords campaign. Do AdWords add somwthing to the url concerning that can help?

SimonW
  • 6,175
  • 4
  • 33
  • 39
  • 1
    possible duplicate of [Determining Smartphone / Non-Smartphone for php](http://stackoverflow.com/questions/1540467/determining-smartphone-non-smartphone-for-php) –  Nov 19 '12 at 09:07
  • 2
    refere to this [http://code.google.com/p/php-mobile-detect/](http://code.google.com/p/php-mobile-detect/) if it can help u.. – Pankit Kapadia Nov 19 '12 at 09:07

3 Answers3

3

I've been using this for a project. I believe I found this code here on stackoverflow.com about a year ago. You can modify the if statement to only redirect specific devices or redirect to specific sites for each. This is a catch all and redirects any mobile device to the http://www.example.com/mobile page. I placed it in a function that is called in the header of every page (works because every page I have includes the same header.php).

$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
$ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");

if($iphone || $android || $palmpre || $ipod || $berry || $ipad == true) 
{ 
    header('Location: mobile');
}

I've never used AdWords but I found this page detailing the addition of GET variables to URLs: Using AdWords Dynamic Parameters in Links

3

I've looked over the user agent strings at:

http://www.useragentstring.com/

And honestly, the simplest way seems to be to check if $_SERVER['HTTP_USER_AGENT'] contains the word "Mobile" or not.

Now, granted, this detects any mobile device, smartphone or tablet, so it may not be perfect for your case, but it seems to be the largest distinction for me as a Web Developer--is my user on a modern "mobile" browser, or are they on a PODB (plain old desktop browser)? :)

1

This information is already in $_SERVER superglobal. All you gotta do is to find occurrence of Iphone/Ipdad etc within $_SERVER['HTTP_USER_AGENT']

Just define some function that will do it for you, then make a redirect to particular page, like this (This is procedural approach):

<?php

$mobile = array('Iphone', 'Androind'); //etc add more


//We won't use global keyword
//We would pass an array as arg instead
function isMobile(array $mobile){
  foreach($mobile as $agent){

     if ( strpos($_SERVER['HTTP_USER_AGENT'], $agent) ){
         //mobile detected
         //or return its name, do it the way you like
         return true;
     }
  }
}


//Now simply check then do redirect, like this
if ( isMobile($mobile) ){

   header('Location: /some-mobile-page.php')

} else {
  header('Location: /regular-page.php');
}
Yang
  • 8,580
  • 8
  • 33
  • 58