Example of function for judging whether it is accessed by mobile phone or PC by PHP

  • 2021-08-28 19:39:00
  • OfStack

Preface

Recently, when developing the project, the PC terminal and the mobile phone terminal were developed respectively, which need to be realized. When accessing the WWW domain name of the PC terminal with the mobile phone, it will automatically judge to jump to the mobile terminal, and when accessing the mobile phone website of the M domain name with the computer, it will automatically jump to the PC website, so there is the following judgment function:

Sample code:


/**
 *  Mobile terminal judgment 
 */
function isMobile()
{ 
 //  If there is HTTP_X_WAP_PROFILE Then 1 It must be a mobile device 
 if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
 {
 return true;
 } 
 //  If via Information contains wap Then 1 It must be a mobile device 
 if (isset ($_SERVER['HTTP_VIA']))
 { 
 //  Cannot find the flase, Otherwise; Otherwise true
 return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
 } 
 //  Brain-dead method, judging the client flag sent by mobile phone , 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'
  ); 
 //  From HTTP_USER_AGENT Find the keywords of mobile browser in 
 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
 {
  return true;
 } 
 } 
 //  Protocol method, because it may be inaccurate, put it in the final judgment 
 if (isset ($_SERVER['HTTP_ACCEPT']))
 { 
 //  If you only support wml And does not support html That 1 It must be a mobile device 
 //  If supported wml And html But wml In html Before that, it was 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;
} 

PHP isset function action

The isset function detects whether a variable is set.

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

Return value:

Returns FALSE if the variable does not exist If the variable exists and its value is NULL, FALSE is also returned If the variable exists and the value is not NULL, TURE is returned When multiple variables are checked at the same time, TRUE is returned only when each item meets the requirements of the previous item, otherwise the result is FALSE If a variable has been released using unset (), it will no longer be isset (). If you test a variable set to NULL using isset (), FALSE will be returned. Also note that one NULL byte ("\ 0") is not equal to the NULL constant of PHP.

Warning: isset () can only be used with variables because passing any other parameters will cause parsing errors. If you want to check whether the constant has been set, you can 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'  Equal to  NULL So it is considered unassigned. 
//  If you want to test  NULL  Key value, you can try the following method. 
var_dump( array_key_exists('hello', $a) ); // TRUE
?>

Summarize


Related articles: