简体   繁体   中英

How to switch the visitor to a page if he logged to the website by mobile and another page if logged from PC?

How to Detect if the visitor is logging by mobile and turn him to index.php and when logging from PC turn him to index.html

is that by .htaccess or what?

You need to use some sort of information provided by the client to determine if the user is on a mobile device or not.

There any many ways to do this, for example in JavaScript

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    /* User is mobile... */
}

Which will check for a string used in a mobile device in the user agent.

There are libraries such as Modernizr ( http://www.modernizr.com/ ) which have things like this to help you.

All in all, there's no set way to do it - you'll have to try different methods and choose the one you like.

You can use these rules in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# forward mobile users to index.php
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC]
RewriteRule ^ - [E=ISMOBILE:1]

RewriteCond %{ENV:ISMOBILE} =1
RewriteRule !^index\.php$ index.php [L]

# otherwise desktop users to index.html
RewriteCond %{ENV:ISMOBILE} !=1
RewriteRule !^index\.html$ index.html [L]

It can be done with this php script, it will return a true if mobile, some mobile user agents are missing

<?php
    // ------- DETECT USER DEVICE ----------
    $user_device = "";
    $IsMobile = "";
    $agent = $_SERVER['HTTP_USER_AGENT'];
    if (preg_match("/Valve/", $agent)) {
       $user_device = "Steam GameOverlay";
    } else if (preg_match("/Safari/", $agent)) {
        $user_device = "Safari";
    } else if (preg_match("/Android/", $agent)) {
        $user_device = "Android Mobile";
    } else if (preg_match("/IEMobile/", $agent)) {
        $user_device = "Windows Mobile";
    } else if (preg_match("/Chrome/", $agent)) {
        $user_device = "Google Chrome";
    } else if (preg_match("/MSIE/", $agent)) {
        $user_device = "Internet Explorer";
    } else if (preg_match("/Firefox/", $agent)) {
        $user_device = "Firefox";
    } else if (preg_match("/Opera/", $agent)) {
        $user_device = "Opera";
    }
    $OSList = array
    (
            // Match user agent string with operating systems
            'Android' => 'Android',
            'Windows 3.11' => 'Win16',
            'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
            'Windows 98' => '(Windows 98)|(Win98)',
            'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
            'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
            'Windows Server 2003' => '(Windows NT 5.2)',
            'Windows Vista' => '(Windows NT 6.0)',
            'Windows Phone' => '(XBLWP7)|(ZuneWP7)|(Windows Phone OS 7.5)|(Windows Phone OS 7.0)|(Windows Phone 8.0)',
            'Windows 8' => '(Windows NT 6.2)',
            'Windows 7' => '(Windows NT 6.1)|(Windows NT 7.0)',
            'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
            'Windows ME' => 'Windows ME',
            'Open BSD' => 'OpenBSD',
            'Sun OS' => 'SunOS',
            'Linux' => '(Linux)|(X11)',
            'iPhone' => 'iPhone',
            'iPad' => 'iPad',
            'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
            'QNX' => 'QNX',
            'BeOS' => 'BeOS',
            'OS/2' => 'OS/2',
            'Mac OS' => 'Mac OS',
            'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
    );

    // Loop through the array of user agents and matching operating systems
    foreach($OSList as $CurrOS=>$Match) {
            // Find a match
            if (@eregi($Match, $agent)) {
                    break;
            } else {
                $CurrOS = "Ukendt OS";
            }
    }
    if ($user_device == ""){
    $user_device = "Ukendt Browser";
    }
    //$device = "$user_device : $CurrOS";
    $device = "$CurrOS";
    // ------- END DETECT USER DEVICE ----------

    if ($CurrOS == "Android" || $CurrOS == "Windows Phone" || $CurrOS == "iPhone"){
        $IsMobile = "True ".$CurrOS; 
    }else{
        $IsMobile = "False ".$CurrOS;
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM