HTTP_USER_AGENT是获取用户浏览器信息的一个全局变量,我们可以利用HTTP_USER_AGENT获取的值来判断用户是手机浏览器还是电脑访问网,代码如下:
- $uAgent = $_SERVER['HTTP_USER_AGENT'];
-
- $osPat = "mozilla|m3gate|winwap|openwave|Windows NT|Windows 3.1|95|Blackcomb|98|ME|X Window|ubuntu|Longhorn|AIX|Linux|AmigaOS|BEOS|HP-UX|OpenBSD|FreeBSD|NetBSD|OS/2|OSF1|SUN";
-
- if(preg_match("/($osPat)/i", $uAgent ))
-
- {
-
- echo "电脑访问";
-
- }
-
- else
-
- {
-
- echo "手机访问";
-
- }
-
- echo '<br/>'.$uAgent;
现在我们事完整的把实例写成一个文件,代码如下:
- $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
-
- $uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile|wap)/i";
-
- if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
- {
- $Loaction = 'wap/';
-
- if (!emptyempty($Loaction))
- {
- ecs_header("Location: $Loactionn");
-
- exit;
- }
-
- }
-
-
-
-
-
-
-
-
-
- function ecs_header($string, $replace = true, $http_response_code = 0)
- {
- if (strpos($string, '../upgrade/index.php') === 0)
- {
- echo '<script type="text/javascript">window.location.href="' . $string . '";</script>';
- }
- $string = str_replace(array("r", "n"), array('', ''), $string);
-
- if (preg_match('/^s*location:/is', $string))
- {
- @header($string . "n", $replace);
-
- exit();
- }
-
- if (emptyempty($http_response_code) || PHP_VERSION < '4.3')
- {
- @header($string, $replace);
- }
- else
- {
- @header($string, $replace, $http_response_code);
- }
- }
javascript中采用navigator.userAgent 来获取,但非智能机似乎不支持js,支持js的移动客户端还可以用以下代码简单判断是不是windows电脑访问的,代码如下:
- if(navigator.platform.indexOf('Win32')!=-1){
-
- }else{
-
- }
|