如何使用PHP将桌面网站从桌面移动到桌面上

时间:2014-08-05 02:18:27

标签: php redirect mobile web desktop

如果桌面用户打开移动网站,代码会将用户重定向到桌面网站。

<?php
// Apple iOS devices
    $iPhone = strpos($_SERVER['HTTP_USER_AGENT'],'iPhone');
    $iPod = strpos($_SERVER['HTTP_USER_AGENT'],'iPod');
    $iPad = strpos($_SERVER['HTTP_USER_AGENT'],'iPad');

// Android devices
    $Android = strpos($_SERVER['HTTP_USER_AGENT'],'Android');

// All mobile devices
    $Mobile = strpos($_SERVER['HTTP_USER_AGENT'],'Mobile');

// BlackBerry devices
    $BlackBerry = strpos($_SERVER['HTTP_USER_AGENT'],'BlackBerry');

// Nokia SymbianOS devices
    $Symbian = strpos($_SERVER['HTTP_USER_AGENT'],'Symbian');

// HTC devices
    $HTC = strpos($_SERVER['HTTP_USER_AGENT'],'HTC_');  

// Windows Phone devices
    $WP7 = strpos($_SERVER['HTTP_USER_AGENT'],'WP7');
    $WP8 = strpos($_SERVER['HTTP_USER_AGENT'],'WP8');

// Browser's OS devices
    $webOS = strpos($_SERVER['HTTP_USER_AGENT'],'webOS');

// Mobile browser (Opera, Firefox)
    $Opera_M = strpos($_SERVER['HTTP_USER_AGENT'],'Opera M');
    $Fennec = strpos($_SERVER['HTTP_USER_AGENT'],'Fennec/');

if ($iPhone || $iPod || $iPad || $Android || $Mobile || $BlackBerry || $Symbian || $HTC || $WP7 || $WP8 || $webOS || $Opera_M || $Fennec == true) 
{ 
header('Location: http://m.website.com/');
//OR
echo "<script>window.location='http://m.website.com'</script>";
}
?>

我可以添加if和else来将用户重定向到桌面网站,例如

else {header('Location: http://website.com/');
//OR
echo "<script>window.location='http://website.com'</script>";
}

其他部分似乎不起作用,因为桌面和移动设备上的浏览器显示太多重定向错误。 感谢

1 个答案:

答案 0 :(得分:0)

假设:

function isMobile() {
    /* return true if is on tablet / phone */
}

在移动版上:

if (!isMobile()) {
    header('Location: website.com');
    exit;
}

在桌面版上:

if (isMobile()) {
    header('Location: m.website.com');
    exit;
}