Mobile.php
4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Created by PhpStorm.
* User: haishengshen
* Date: 14-3-6
* Time: 下午1:15
*/
namespace WebPlugin;
class Mobile
{
/**
* 获取用户User-Agent
* @return bool
*/
public static function getUserAgent()
{
if (!isset($_SERVER['HTTP_USER_AGENT']) || empty($_SERVER['HTTP_USER_AGENT'])) {
return '';
}
return $_SERVER['HTTP_USER_AGENT'];
}
/**
* 获取当前地址
* @return array
*/
public static function getNowUrl()
{
return array('host'=>$_SERVER['HTTP_HOST'], 'uri'=> $_SERVER['REQUEST_URI']);
}
/**
* 获取手机版的对应地址
* @param $host
* @return mixed
*/
public static function newHost($host)
{
if ($host == 'www.yohobuy.com' || $host == 'new.yohobuy.com' || stristr($host, 'new.yohobuy.com')) {
return str_replace(array('www.yohobuy', 'new.yohobuy'), 'm.yohobuy', $host);
}
if ($host == 'huodong.yohobuy.com') {
return str_replace('huodong.yohobuy', 'm.yohobuy', $host);
}
return str_replace('yohobuy.com', 'm.yohobuy.com', $host);
}
public static function match($url, $host, $uri)
{
// //guang.yohobuy.com
// //guang.yohobuy.com/1.html
// $list = array(
// array(
// 'match' => '#^([guang\.]*)yohobuy\.com(\/*)$#',
// ),
// array(
// 'match' => '#([guang\.]*)yohobuy.com/([0-9]+).html#',
// 'to' => 'guang.m.yohobuy.com/info/index?id='.substr($uri,1,(strpos($uri,'.')-1)) ,
// ),
// );
// foreach ($list as $value) {
// if (preg_match($value['match'], $url) && !empty($value['to'])) {
// return $value['to'];
// } else if (preg_match($value['match'], $url)) {
// $newHost = self::newHost($host);
// return $newHost . $uri;
// }
// }
//
// $continueList = array('www', 'list', 'search');
// $hostSplit = explode('.', $host);
return 'm.yohobuy.com';
}
/**
* 获取新的URL
* @return string
*/
public static function getNewUrl()
{
$url = self::getNowUrl();
return self::match($url['host'].$url['uri'], $url['host'], $url['uri']);
}
/**
* 判断是否手机
* @return bool
*/
public static function isMobile()
{
$userAgent = self::getUserAgent();
if (stristr($userAgent, 'ipad')) {
return false;
}
$mobileAgents = array('iphone','android',"240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
$isMobile = false;
foreach ($mobileAgents as $device) {
if (stristr($userAgent, $device)) {
$isMobile = true;
break;
}
}
return $isMobile;
}
/**
* 是否转到手机版
* @return bool
*/
public static function isGoMobile()
{
if (empty($_COOKIE['m2w']) && self::isMobile()) {
$newUrl = self::getNewUrl();
Header("HTTP/1.1 301 Moved Permanently");
header('Location:http://'.$newUrl);
exit;
}
return true;
}
}