Example of a function using PHP to determine whether it is accessed from a mobile phone or a PC _php skills

韦小宝
Release: 2023-03-17 22:44:01
Original
2034 people have browsed it

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[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) !== false) && (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;) === false || (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) < strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;))))
 {
  return true;
 } 
 } 
 return false;
}
Copy after login


##PHP isset function function

isset function is to detect whether the variable is set.

Format:

bool isset (mixed var [, mixed var [, ...]] )

Return value:

  • If the variable does not exist, it returns FALSE

  • If the variable exists and its value is NULL, it also returns FALSE

  • If the variable exists and the value is not NULL, TRUE will be returned

  • When checking multiple variables at the same time, TRUE will be returned only when each single item meets the previous requirement, otherwise the result will be FALSE

  • If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("\0") is not equivalent to PHP's NULL constant.

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 (&#39;test&#39; => 1, &#39;hello&#39; => NULL);
var_dump( isset ($a[&#39;test&#39;) ); // TRUE
var_dump( isset ($a[&#39;foo&#39;) ); // FALSE
var_dump( isset ($a[&#39;hello&#39;) ); // FALSE
// &#39;hello&#39; 等于 NULL,所以被认为是未赋值的。
// 如果想检测 NULL 键值,可以试试下边的方法。
var_dump( array_key_exists(&#39;hello&#39;, $a) ); // TRUE
?>
Copy after login


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!

Related labels:
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