PHP function implementation to determine whether mobile terminal access_PHP tutorial

WBOY
Release: 2016-07-13 10:04:48
Original
918 people have browsed it

PHP function implementation to determine whether mobile access is available

This article shares with you a PHP function to determine whether mobile access is available. It was collected before and is posted here. Recommended To my friends.

I forgot where I got the function. I accidentally found it in a package and saved it temporarily

The code is as follows:


/**
* Whether to access via mobile terminal
*
* @return bool
*/
function isMobile()
{
// If there is HTTP_X_WAP_PROFILE, 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']))
{
// If not found, it is false, otherwise it is true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
// Brainless method, determine 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'
);
// Find keywords for mobile browsers from HTTP_USER_AGENT
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
{
return true;
}
}
// Agreement method, because it may be inaccurate, leave it to the final judgment
if (isset ($_SERVER['HTTP_ACCEPT']))
{
// If it only supports wml and does not support html, it must be a mobile device
// If wml and html are supported but wml comes before html, it is a mobile device
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;
}

A very simple and practical function, shared with everyone, I hope you will like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/964007.htmlTechArticlePHP function implementation to determine whether mobile access is available. This article will share with you a PHP function to determine whether mobile access is available. , I collected it before and posted it here to recommend it to my friends. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!