Php determines whether it is a mobile device, php determines the device
<?php
function
isMobile()
{
// If HTTP_X_WAP_PROFILE exists, it must be a mobile device.
if
(isset (
$_SERVER
[
'HTTP_X_WAP_PROFILE'
]))
{
return
true;
}
// If the via information contains wap, it must be a mobile device. Some service providers will block this information.
if
(isset (
$_SERVER
[
'HTTP_VIA'
]))
{
// Flase cannot be found; otherwise, true
return
stristr
(
$_SERVER
[
'HTTP_VIA'
],
"wap"
) ? true : false;
}
// Identify the client flag sent by the mobile phone. The compatibility needs to be improved.
if
(isset (
$_SERVER
[
'HTTP_USER_AGENT'
]))
{
$clientkeywords
=
array
(
'nokia'
,
'sony'
,
'ericsson'
,
'mot'
,
'samsung'
,
'htc'
,
'sgh'
,
'lg'
,
'sharp'
,
'sie-'
,
'philips'
,
'panasonic'
,
'alcatel'
,
'lenovo'
,
'iphone'
,
'ipod'
,
'blackberry'
,
'meizu'
,
'android'
,
'netfront'
,
'symbian'
,
'ucweb'
,
'windowsce'
,
'palm'
,
'operamini'
,
'operamobi'
,
'openwave'
,
'nexusone'
,
'cldc'
,
'midp'
,
'wap'
,
'mobile'
);
// Search for the keyword of the mobile browser from HTTP_USER_AGENT
if
(preg_match(
"/("
. implode(
'|'
,
$clientkeywords
) .
")/i"
,
strtolower
(
$_SERVER
[
'HTTP_USER_AGENT'
])))
{
return
true;
}
}
// Resolution, which may be inaccurate and placed in the final judgment
if
(isset (
$_SERVER
[
'HTTP_ACCEPT'
]))
{
// If only wml is supported and html is not supported, it must be a mobile device.
// If wml and html are supported, but wml is a mobile device before html
if
((
strpos
(
$_SERVER
[
'HTTP_ACCEPT'
],
'vnd.wap.wml'
) !== false) && (
strpos
(
$_SERVER
[
'HTTP_ACCEPT'
],
'text/html'
) === false || (
strpos
(
$_SERVER
[
'HTTP_ACCEPT'
],
'vnd.wap.wml'
) <
strpos
(
$_SERVER
[
'HTTP_ACCEPT'
],
'text/html'
))))
{
return
true;
}
}
return
false;
}
?>