SoFunction
Updated on 2025-03-02

Example of function to determine whether it is accessed by mobile phone or PC using PHP

Preface

Recently, when developing projects, the PC and mobile phones were developed separately, which need to be implemented. When accessing the PC WWW domain name with a mobile phone, it will automatically jump to the mobile terminal. When accessing the M domain name mobile website with a computer, it will automatically jump to the PC website. So the following judgment function is available:

Sample code:

/**
  * Mobile judgment
  */
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 if (isset ($_SERVER['HTTP_VIA']))
 { 
 // Not found as flase, otherwise it is true return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
 } 
 // Brain-dead method, to determine the client logo sent by the 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'
  ); 
 // 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, let it be final judgment if (isset ($_SERVER['HTTP_ACCEPT']))
 { 
 // If you only support wml and do not support html, 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'], '') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], '') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
 {
  return true;
 } 
 } 
 return false;
} 

PHP isset function function

The isset function detects 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 will also return FALSE.
  • If the variable exists and the value is not NULL, then TURE is returned.
  • When multiple variables are checked at the same time, TRUE will be returned only if each 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 set to NULL, FALSE will be returned. Also note that a NULL byte ("\0") is not equivalent to the NULL constant of PHP.

warn:isset() can only be used for variables, as passing any other parameters will cause parsing errors. To detect whether the 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' is equal to NULL, so it is considered unassigned.// If you want to check the NULL key value, you can try the following method.var_dump( array_key_exists('hello', $a) ); // TRUE
?>

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.