2

I have a Web Application which is browser compatible made in php and HTML5. I have a certain requirement when a client browse the site from Ipad I want to display different banner.

<?php $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
   if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
$android = '1';

     }
     if(stripos($ua,'iPhone') !== false  || stripos($ua,'iPod') !== false  ||     strstr($ua,'iPhone') || strstr($ua,'iPod')  ) { 

$iPhone = '1';

 }

 ?>

This condition is working for android mobile and other devices but not for Ipad. Does anyone have idea how to detect Ipad device in php.

Amar Banerjee
  • 4,992
  • 5
  • 34
  • 51

2 Answers2

5
    if (preg_match('/ipad/i', $ua)) 
    {
        $platform = 'iPad';
    }

here is what i use for rest of them:

    if (preg_match('/linux/i', $ua)) 
    {
        $platform = 'Linux';
    }
    elseif (preg_match('/ubuntu/i', $ua)) 
    {
        $platform = 'Ubuntu';
    }
    elseif (preg_match('/macintosh|mac os x/i', $ua)) 
    {
        $platform = 'Mac';
    }
    elseif (preg_match('/windows|win32/i', $ua)) 
    {
        $platform = 'Windows';
    }

    if (preg_match('/android/i', $ua)) 
    {
        $platform = 'Android';
    }

    if (preg_match('/palm/i', $ua)) 
    {
        $platform = 'Palm';
    }


    if (preg_match('/iphone/i', $ua)) 
    {
        $platform = 'iPhone';
    }

    if (preg_match('/blackberry/i', $ua)) 
    {
        $platform = 'Blackberry';
    }

    if (preg_match('/ipod/i', $ua)) 
    {
        $platform = 'iPod';
    }

    if (preg_match('/ipad/i', $ua)) 
    {
        $platform = 'iPad';
    }
GGio
  • 7,563
  • 11
  • 44
  • 81
  • 1
    Not sure OP needs a regular expression for this. I think `stripos` would work fine. – bozdoz Jun 06 '13 at 15:52
  • I rely on regex more than strpos :D – GGio Jun 06 '13 at 15:56
  • 2
    Why don't you use something like `$regex = '/android|blackberry|iphone/i';` then `preg_match($regex, $ua, $platform);` Then $platform[0] should equal the platform, or be empty. – bozdoz Jun 06 '13 at 15:57
  • does not work with i phone 6 - I know this is like 2 years old but does any one have an updated pregmatch – mindmyweb Aug 30 '15 at 06:43
2

I believe the user agent has 'ipad' in it. Search for ipad.

If you're curious, use a page to spit out the HTTP USER AGENT, and visit with an ipad:

<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>
bozdoz
  • 12,550
  • 7
  • 67
  • 96