如果他通过手机登录到网站,如果从PC登录到另一个页面,如何将访问者切换到页面?

时间:2014-01-09 10:20:46

标签: php html .htaccess mobile url-redirection

如何检测访问者是否通过移动设备登录并将其转为index.php 当从PC登录时,将他转为index.html

是.htaccess还是什么?

3 个答案:

答案 0 :(得分:1)

您需要使用客户提供的某种信息来确定用户是否在移动设备上。

有很多方法可以做到这一点,例如

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

将检查用户代理中移动设备中使用的字符串。

像Modernizr(http://www.modernizr.com/)这样的库有这样的东西可以帮到你。

总而言之,没有固定的方法 - 你必须尝试不同的方法并选择你喜欢的方法。

答案 1 :(得分:0)

您可以在DOCUMENT_ROOT/.htaccess文件中使用这些规则:

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]

答案 2 :(得分:0)

可以使用这个php脚本完成,如果移动,它将返回true,一些移动用户代理丢失

<?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;
    }
?>