Use PHP to determine whether the mobile phone or PC Access is a function example, pc example

Source: Internet
Author: User

Use PHP to determine whether the mobile phone or PC Access is a function example, pc example

Preface

Recently, in the Development Project, the PC and mobile terminals were developed respectively. To achieve this, when accessing the WWW domain name on the PC end with a mobile phone, the system automatically determines that the domain name is redirected to the mobile end, when you access the mobile website of the M domain name on your computer, the system automatically jumps to the PC-side website. Therefore, the following judgment function is provided:

Sample Code:

/*** Mobile terminal judgment */function isMobile () {// if HTTP_X_WAP_PROFILE exists, 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 ']) {// cannot be found as flase; otherwise, it is true return stristr ($ _ SERVER ['HTTP _ vean'], "wap ")? True: false;} // identify the client flag sent by the mobile phone. The 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', 'cld C ', 'midp', 'wap ', 'mobile'); // search for the keyword if (preg_match ("/(". implode ('|', $ clientkeywords ). ")/I", strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']) {return true ;}// resolution method, because it may be inaccurate, put it at the end to determine if (isset ($ _ SERVER ['HTTP _ ACCEPT ']) {// if only wml is supported and html is not supported, it must be a mobile device // if wml and html are supported, but wml is a mobile device before html (strpos ($ _ SERVER ['HTTP _ ACCEPT '], 'vnd. wap. wml ')! = False) & (strpos ($ _ SERVER ['HTTP _ ACCEPT '], 'text/html ') === false | (strpos ($ _ SERVER ['HTTP _ ACCEPT '], 'vnd. wap. wml ') <strpos ($ _ SERVER ['HTTP _ ACCEPT'], 'text/html ') {return true ;}} return false ;}

Functions of the PHP isset Function

The isset function checks whether variables are set.

Format:bool isset ( mixed var [, mixed var [, ...]] )

Return Value:

  • If the variable does not exist, FALSE is returned.
  • If the variable exists and its value is NULL, FALSE is returned.
  • If the variable exists and the value is not NULL, true is returned.
  • When multiple variables are checked at the same time, TRUE is returned only when each individual item meets the previous requirement; otherwise, the result is FALSE.
  • If unset () is used to release a variable, it will no longer be isset (). If you use isset () to test a variable that is set to NULL, FALSE is returned. Note that a NULL byte ("\ 0") is not equivalent to the NULL constant of PHP.

Warning:Isset () can only be used for variables, because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () function.

<? Php $ a = array ('test' => 1, 'Hello' => NULL); var_dump (isset ($ a ['test ')); // TRUEvar_dump (isset ($ a ['foo'); // FALSEvar_dump (isset ($ a ['hello ')); // FALSE // 'Hello' is NULL, so it is considered to be unassigned. // If you want to check the NULL key value, try the following method. Var_dump (array_key_exists ('hello', $ a); // TRUE?>

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

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.