简体   繁体   English

如果访客通过移动设备登录到网站,如何将其切换到页面;如果通过PC登录,则如何切换到另一个页面?

[英]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 如何检测访问者是否正在通过移动设备登录并将其转到index.php,以及从PC登录时将其转到index.html

is that by .htaccess or what? 是.htaccess还是什么?

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 有很多方法可以做到这一点, 例如在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. 有诸如Modernizr( http://www.modernizr.com/ )之类的库,这些库可以帮助您。

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: 您可以在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]

It can be done with this php script, it will return a true if mobile, some mobile user agents are missing 可以使用此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;
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果他没有登录,如何防止用户访问主页? - how to prevent user from access to main page if he wasn't logged in? 一旦管理员登录wordpress,如何在网站访问者的两种形式之间切换 - how to switch between two form for website visitor once admin logged in wordpress 在用户登录 php 后,我如何成功地将用户重定向到他所在的位置(他的当前页面)? - how can i successfully redirect user to where he was(his current page) after he logged in in php? 如何向网站访问者隐藏页面URL? - how to hide page URLs from the website visitor? 如果未登录,将用户重定向到另一个页面? - Redirect user to another page if not logged in? 让用户在再次访问同一页面时登录? - keep user logged in when he visit the same page again? 用户是否已经登录。 然后应该将他移到主页但不能工作 - If a user is already logged on. then he should be moved to home page but not working 当Symfony2中的登录用户访问登录页面时,该如何重定向他? - How can I redirect logged in user in Symfony2 when he visits the login page? 启用PHP会话的页面如何知道用户从另一个页面登录? - How does a PHP session-enabled page know a user logged in from another page? 如果登录,则链接到一个页面;如果未登录,则链接到另一个页面 - Link to one page if logged in and another page if not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM