This article mainly for you to introduce the PHP automatic recognition of the current use of mobile terminals, with a certain reference value, interested in small partners can refer to
Although now using jquery can accurately determine the current use of the kind of client, but sometimes according to the function and demand, we may need to use PHP to determine the current program running environment, jquery here do not say, here directly about how PHP is implemented, I hope to be helpful.
We first determine whether the current operating environment is the PC (computer side) or SP (mobile phone, ipad side)
Class Self_test { const PC = ' PC '; Const SP = ' SP '; Private $_splist = Array (' IPhone ', ' Android ', ' iPod ', ' iPad ', ' Tizen ');//Set up a frequently used SP terminal, temporarily commonly used SP side on these kinds, if any, we can also increase Private $terminal; Public Function __construct () { $this->setterminal ();//Gets the value of the $terminal variable through the Setterminal () method }/ * * Function setterminal () * Get terminal information * @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::P C; } /* Function pc_sp () * Output terminal information * @return String * /Public Function pc_sp () { return $ this->terminal; } } $str = new Self_test (); Echo $str->pc_sp ();//output current customer is using a PC or 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 ');//Set up a frequently used SP terminal, temporarily commonly used SP side on these kinds, if any, we can also increase Private $environment; Public Function __construct () { $this->setenvironment ();//Gets the value of the $terminal variable through the Setenvironment () method } / * function environment () * Output terminal information * @return String * /Public function environment () { return $this->environment; } / * Function setenvironment () * Get terminal information * @return String * /Private Function setenvironment ( { $isSp = self::P c;//If it is a PC, it is not necessary to determine whether it is Android or Apple, so only the output PC can be 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 ();//output current terminal used by customer
The above code is the success of their own after the post to share the content, so you can debug in their own environment.
Articles you may be interested in:
Phpstorm method for local breakpoint debugging
Basic techniques of PHP+MARIADB database operation