跳至正文

PHP 识别客户端类型跳转不同路径

  • 后端

根据客户端类型,将URL跳转到指定的路径上,当用户通过移动端访问PC端网址时,能跳转到移动端的URL上。

实现代码如下:

<?php
//识别客户端
$agent = $_SERVER['HTTP_USER_AGENT'];
if(strpos($agent,"comFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")){
	header("Location:". str_replace("/pc/","/m/",$_SERVER['REQUEST_URI']));
}
?>

其中 /pc/ 为 PC 端路径,/m/ 为移动端路径。

网站首页文件根据客户端类型跳转到指定路径,这样用户在移动端输入域名时,可进入移动端页面。

实现代码如下:

<?php
//识别客户端
$agent = $_SERVER['HTTP_USER_AGENT'];
if(strpos($agent,"comFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")){
	header("Location:/m/");
} else {
	header("Location:/pc/");
}
?>
标签: