PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Detecting Mobile Devices for redirection

Go down

Detecting Mobile Devices for redirection Empty Detecting Mobile Devices for redirection

Post by jamied_uk 1st December 2013, 21:21

Code:
<?php

function check_user_agent ( $type = NULL ) {

        $user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );

        if ( $type == 'bot' ) {

                // matches popular bots

                if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {

                        return true;

                        // watchmouse|pingdom\.com are "uptime services"

                }

        } else if ( $type == 'browser' ) {

                // matches core browser types

                if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {

                        return true;

                }

        } else if ( $type == 'mobile' ) {

                // matches popular mobile devices that have small screens and/or touch inputs

                // mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America

                // detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here

                if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {

                        // these are the most common

                        return true;

                } else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {

                        // these are less common, and might not be worth checking

                        return true;

                }

        }

        return false;

}

 
?>

How to use:

 
<?php

$ismobile = check_user_agent('mobile');

if($ismobile) {

header( "Location: /mobile/");

} else {

header( "Location: /index.php");

exit();

}

?>
jamied_uk
jamied_uk
Admin

Posts : 2950
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Detecting Mobile Devices for redirection Empty Re: Detecting Mobile Devices for redirection

Post by jamied_uk 1st December 2013, 21:23

You will have to name the file correctly (example index.php and your old index.php will become index1.php) or you maybe stuck in an endless loop but i have tested this and it works Very Happy
jamied_uk
jamied_uk
Admin

Posts : 2950
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum