1

I got a function that works well, but i need to extend it a bit, and im not a programmer.

Now it can detect ios well, and android and web are both redirected to one page for both, web and android.

The goal is, to redirect ios to page 1, android to page 2 and web-os to page 3.

 function find_mobile_browser() {
    if(preg_match('/(iphone|ipad|ipod)/i', $_SERVER['HTTP_USER_AGENT'])) {
        return true;
    } else {
        return false;
    }
}

The call is this:

<?php $mobile_browser = find_mobile_browser(); 
                        if($mobile_browser) { 
                        include("ios.php"); /* if mobile browser detected, do this */ 
                        } else { 
                        include("android_and_web.php"); /* else do this */ 
                        }
                        ?>

Can you help me to extend this snippet to detect it so, that the call can this:

<?php $mobile_browser = find_mobile_browser(); 
                        if($mobile_browser) { 
                        include("ios.php"); /* if ios detected, do this */ 
                        } else { 
                        include("android.php"); /* if android detected, do this */ 
                        }

                        } else { 
                        include("web_os.php"); /* if web-os detected, do this */ 
                        }
                        ?>

I think its not a perfect way to detect mobile devices, but is there a better one?

Thanks, Orwell

After your nice answers, i'll explain maybe better.

The "header" answer is useful for me, - but im not a programmer and so i dont get it to work how i want it to use. The redirection works well, but i need no redirections, i need includes. I want to include depending ios/android/web a little text snippet (the php-files) in some of my templates.

The function works well, but my call didnt show any result. Here ho i try to call the function:

    <?php
    $mobile_browser = find_mobile_browser();

    if($mobile_browser == 'ios')
    {
        include("ios.php");
    }
    elseif ($mobile_browser == 'android')
    {
        include("android.php");
    }
    else
    {
        /* if no mobile os detected, include "web.php" */
    }

?>

Hope im clearer now, and you are not to upset.

Thanks in advance, Orwell

Thanks for your greatfull answers, - i got it now :))

Cheers, Orwell

Orwell
  • 11
  • 2

3 Answers3

1

In order to redirect you can use header

header('Location:'.$your_url);

So the code would become something like (depending on the value of $mobile_browser)

function find_mobile_browser()
{
    if(preg_match('/(iphone|ipad|ipod)/i', $_SERVER['HTTP_USER_AGENT']))
    {
        return 'ios';
    }
    elseif (preg_match('/(android)/i', $_SERVER['HTTP_USER_AGENT']))
    {
        return 'android';
    }
    else
    {
        return false;
    }
}

<?php
    $mobile_browser = find_mobile_browser();

    $ios_url = 'http://www.example.com';
    $android_url = 'http://www.example.com';
    $web_url = 'http://www.example.com';

    if($mobile_browser == 'ios')
    {
        header('Location:'.$ios_url);
        exit;
    }
    elseif ($mobile_browser == 'android')
    {
        header('Location:'.$android_url);
        exit;
    }
    else
    {
        header('Location:'.$web_url);
        exit;
    }

?>

sitilge
  • 3,687
  • 4
  • 30
  • 56
  • Does `find_mobile_browser()` return `android` or `ios` ? I don't think so. Also, why are you `including` files if after you use `Location: `? Sorry, but your code doesn't make any sense. – Pedro Lobito Apr 20 '15 at 07:11
  • @PedroLobito Ok, see the update now: added the extended snippet and removed include. – sitilge Apr 20 '15 at 07:18
1

Use the Mobile-Detect php class.

Example:

<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;

if($detect->isAndroidOS()) {
 include("android.php");
}elseif( $detect->isiOS()) {
 include("ios.php");
}else( $detect->iswebOS()) {
 include("web_os.php");
}
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

You can use PHP Class named: Mobile Detect

Link to download: https://github.com/serbanghita/Mobile-Detect/archive/2.8.12.zip

Here is the sample snippet to use it you can customize it depends upon your need:

<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;

// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {

}

// Any tablet device.
if( $detect->isTablet() ){

}

// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){

}

// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){

}

if( $detect->isAndroidOS() ){

}

$detect->is('Chrome');
$detect->is('iOS');
$detect->is('UC Browser');

$userAgents = array(
'Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
'BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103',
);

foreach($userAgents as $userAgent){

  $detect->setUserAgent($userAgent);
  $isMobile = $detect->isMobile();
  $isTablet = $detect->isTablet();

}

$detect->version('iPad'); // 4.3 (float)
$detect->version('iPhone') // 3.1 (float)
$detect->version('Android'); // 2.1 (float)
$detect->version('Opera Mini'); // 5.0 (float)

?>
Keep Coding
  • 636
  • 9
  • 26