如何自动检测操作系统和重定向

时间:2017-10-25 23:16:52

标签: android linux windows

我有一个网站。我想创建主页 index.php ,它检测用户运行的操作系统是否是移动的,并重定向到移动网站。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

您想要考虑user-agent标头。这是一篇非常完整的文章:

  

https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent

答案 1 :(得分:0)

<html><head>
<script type="text/javascript">
var device = navigator.userAgent
if (device.match(/Iphone/i)|| device.match(/Ipod/i)|| device.match(/Android/i)|| device.match(/J2ME/i)|| device.match(/BlackBerry/i)|| device.match(/iPhone|iPad|iPod/i)|| device.match(/Opera Mini/i)|| device.match(/IEMobile/i)|| device.match(/Mobile/i)|| device.match(/Windows Phone/i)|| device.match(/windows mobile/i)|| device.match(/windows ce/i)|| device.match(/webOS/i)|| device.match(/palm/i)|| device.match(/bada/i)|| device.match(/series60/i)|| device.match(/nokia/i)|| device.match(/symbian/i)|| device.match(/HTC/i))
{
window.location = "./mobile.php";
}
else
{
var page = "./desktop.php";         
top.location = page;
}
</script>
</head>
</html>