PHP automatically recognizes the current use of mobile terminal de related explanations

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.