Home > Backend Development > PHP Tutorial > PHP automatically identifies the mobile terminal currently in use

PHP automatically identifies the mobile terminal currently in use

不言
Release: 2023-03-28 22:52:01
Original
1365 people have browsed it

This article mainly introduces PHP to automatically identify the current mobile terminal in detail, which has certain reference value. Interested friends can refer to it

Although jquery can now be used to accurately determine Which kind of client is currently used, but sometimes depending on the functions and requirements, we may need to use PHP to determine the environment in which the current program is running. I won’t talk about jquery here. Here I will talk about how PHP is implemented. I hope it will be helpful to you. Everyone helps.

Let’s first determine whether the current operating environment is pc (computer) or sp (mobile phone, ipad)

class self_test { 
  
 const PC = 'pc'; 
  
 const SP = 'sp'; 
  
 private $_splist = array('iPhone','Android','iPod','iPad','Tizen');//设置经常使用的sp终端,暂时常用的sp端就这几种,如果有的话大家也可以增加 
 
 private $terminal; 
  
 public function __construct(){ 
  $this->setTerminal();//通过setTerminal()方法获取到$terminal变量的值 
 } 
  
 /* 
  * function setTerminal() 
  * 获取终端信息 
  * @return string 
  */ 
  
 private function setTerminal(){ 
  $isSp = false; 
  foreach($this->_splist as $spname){ 
   if (strstr($_SERVER['HTTP_USER_AGENT'], $spname)) { 
    $isSp = true; 
    break; 
   } 
  } 
  return $this->terminal = ($isSp) ? self::SP : self::PC; 
 } 
  
 /* 
  * function PC_SP() 
  * 输出终端信息 
  * @return string 
  */ 
 public function PC_SP(){ 
  return $this->terminal; 
 } 
} 
 
$str = new self_test(); 
echo $str->PC_SP();//输出目前客户使用的是PC还是SP
Copy after login

2 .Accurately determine the current operating environment and output the client environment

class self_test {  
 const PC = 'pc';  
 const SP = 'sp';  
 private $_splist = array('iPhone','Android','iPod','iPad','Tizen');//设置经常使用的sp终端,暂时常用的sp端就这几种,如果有的话大家也可以增加 
 private $environment;  
 public function __construct(){ 
  $this->setEnvironment();//通过setEnvironment()方法获取到$terminal变量的值 
 } 
  
 /* 
  * function environment() 
  * 输出终端信息 
  * @return string 
  */ 
 public function environment(){ 
  return $this->environment; 
 } 
  
 /* 
  * function setEnvironment() 
  * 获取终端信息 
  * @return string 
  */ 
 private function setEnvironment(){ 
  $isSp = self::PC;//如果是PC端,就不需要判断是安卓还是apple了,所以只输出pc就可以 
  foreach($this->_splist as $spname){ 
   if (strstr($_SERVER['HTTP_USER_AGENT'], $spname)) { 
    $isSp = $spname; 
    break; 
   } 
  } 
  return $this->environment = $isSp; 
 } 
} 
$str = new self_test(); 
echo $str->environment();//输出目前客户使用的终端
Copy after login

The above codes are all posted and shared after successful operation, so everyone You can debug in your own environment.

Related recommendations:

PhpStorm local breakpoint debugging method steps

MySQL read-write separation operation implemented by PHP

The above is the detailed content of PHP automatically identifies the mobile terminal currently in use. For more information, please follow other related articles on the PHP Chinese website!

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