[英]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.