In daily work development, we usually need to develop two different systems, PC and mobile, so as to enter different operation interfaces according to different access terminals. The following article mainly introduces you to the relevant information about using PHP to determine whether the PHPfunction is used to determine whether it is accessed from a mobile phone or a PC. If you don’t understandPHP Friends who have knowledge or need in this area can refer to it.
Preface
Recently when developing the project, we developed the PC side and the mobile phone side respectively. We need to implement it to use the mobile phone to access the PC side WWW. When using a domain name, it will automatically jump to the mobile site. When using a computer to access the mobile website with M domain name, it will automatically jump to the PC site. So we have the following judgment function:
Sample code:
/** * 移动端判断 */ function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) { return true; } // 如果via信息含有wap则一定是移动设备 if (isset ($_SERVER['HTTP_VIA'])) { // 找不到为flase,否则为true return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false; } // 脑残法,判断手机发送的客户端标志,兼容性有待提高 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' ); // 从HTTP_USER_AGENT中查找手机浏览器的关键字 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { return true; } } // 协议法,因为有可能不准确,放到最后判断 if (isset ($_SERVER['HTTP_ACCEPT'])) { // 如果只支持wml并且不支持html那一定是移动设备 // 如果支持wml和html但是wml在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; }
##PHP isset function function
isset function is to detect whether the variable is set. Format:bool isset (mixed var [, mixed var [, ...]] )
Return value:
Warning: isset() can only be used with variables, because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.
<?php $a = array ('test' => 1, 'hello' => NULL); var_dump( isset ($a['test') ); // TRUE var_dump( isset ($a['foo') ); // FALSE var_dump( isset ($a['hello') ); // FALSE // 'hello' 等于 NULL,所以被认为是未赋值的。 // 如果想检测 NULL 键值,可以试试下边的方法。 var_dump( array_key_exists('hello', $a) ); // TRUE ?>
Summary
The above is all the content of this article , I hope it will be helpful to everyone! !Related recommendations:
PHP method to determine whether it is a mobile client
php Detailed explanation of using file_exists() function to determine whether a file exists
php Detailed explanation of code example for determining ip blacklist program
The above is the detailed content of Example of a function using PHP to determine whether it is accessed from a mobile phone or a PC _php skills. For more information, please follow other related articles on the PHP Chinese website!