Determine whether it is mobile phone or computer isMobile () or mobile phone ismobile

Source: Internet
Author: User

Determine whether it is mobile phone or computer isMobile () or mobile phone ismobile

1. Write the method for judging the mobile phone in the PublicController.

<? Phpnamespace Home \ Controller; use Think \ Controller; class PublicController extends Controller {// determine whether it is a mobile phone or a computer function isMobile () {// if there is HTTP_X_WAP_PROFILE, 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. Some service providers will block this information if (isset ($ _ SERVER ['HTTP _ Ve']) {// 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', 'cldc ', '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 ;}}

2. inherit this method from the php controller IndexController. class. php:

<? Phpnamespace Home \ Controller; use Think \ Controller; class IndexController extends PublicController {function _ construct () {// constructor: :__ construct ();}

3. Call and instantiate a method:

/* User homepage */public function Personal () {$ mobile = parent: isMobile (); // instantiate this method if ($ mobile = "true ") {$ this-> display (Personal);} else {$ this-> display (Wap_Personal );}}

 

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.