Now when mobile devices are hot, it may be that we often need to use a page match for mobile devices when doing web front-end development. Of course, we can use the response page design to handle the front-end matching problem, but the response page just does not show some code, but it will still be loaded into the user's mobile device. In order to better take care of the user's mobile browsing experience, reduce unnecessary load. We can then use PHP to reduce the user's code load, improve the user experience when browsing the page. Because of the wide variety of mobile devices and the diversity of browsers, it is only through user-agent that the problem can not be completely solved.
<?php
function IsMobile ()
{
If you have http_x_wap_profile, you 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, and some service providers block the information
if (Isset ($_server[' Http_via '))
{
Not found for flase, otherwise true
Return Stristr ($_server[' Http_via '], "WAP")? True:false;
}
To judge the mobile phone to send the client logo, 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 '
);
Find keywords for mobile browsers from Http_user_agent
if (Preg_match) ("/". Implode (' | ', $clientkeywords). ")/I", Strtolower ($_server[' http_user_agent ')))
{
return true;
}
}
Agreement law because there may be inaccurate and put it to 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;
}
?>
At present, this method can be used to judge most mobile devices, and can be used to add some UA judgment parameters.