Home > Backend Development > PHP Tutorial > PHP实现根据设备类型自动跳转相应页面的方法_PHP

PHP实现根据设备类型自动跳转相应页面的方法_PHP

WBOY
Release: 2016-06-01 11:50:25
Original
892 people have browsed it

随着当今移动设备的普及,上网已经比过去更加方便。针对Android智能手机,iPhone/iPad等移动终端,很多网站都相继推出了针对电脑和这类手机等移动设备访问的网页。本文所述的实例代码就可以实现根据这些移动设备终端而自动跳转到适合它们浏览的页面。即判断PC端或手机站智能端并跳转。

完整的实例代码如下:

<&#63;php
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
$iphone = (strpos($agent, 'iphone')) &#63; true : false;
$ipad = (strpos($agent, 'ipad')) &#63; true : false;
$android = (strpos($agent, 'android')) &#63; true : false;
if($iphone || $ipad)
{
 echo "<script>window.location.href='pc.html'</script>";//这里也可以是网址
}
if($android){
 echo "<script>window.location.href='andorid.html'</script>";//这里也可以是网址
}
&#63;>

Copy after login

这里使用PHP中的内置函数HTTP_USER_AGENT获取客户端设备类型,再使用字符串处理函数对其提取,然后判断是什么设备,根据设备自动定位到适合的网页或页面发送给客户端。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template