Home > Article > Backend Development > PHP automatically identifies the current mobile terminal. Related explanations
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
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();//输出目前客户使用的终端
The above codes are all posted and shared after successful operation, so everyone You can debug in your own environment.
PhpStorm local breakpoint debugging method
Basic skills for PHP MariaDB database operations
The above is the detailed content of PHP automatically identifies the current mobile terminal. Related explanations. For more information, please follow other related articles on the PHP Chinese website!