How does PHP identify website access on a computer or mobile phone?

Source: Internet
Author: User
Access to websites through mobile phones is becoming more and more popular. if we want to count the traffic of websites through PCs and mobile terminals, or we need to do some special processing for mobile terminals, then we need to identify the client of the user accessing the website. The following example uses php to identify whether the user accesses the website through a computer or a mobile phone. & Lt ;? PhpfunctionisMobile () {$ useragentisset, or if you need to do some special processing for the mobile terminal, then we need to identify the client of the user accessing the website, the following example uses php to identify whether a user accesses a website on a computer or on a mobile phone.

 Function isMobile (){
$ Useragent = isset ($ _ SERVER ['http _ USER_AGENT '])? $ _ SERVER ['http _ USER_AGENT ']: '';
$ Useragent_commentsblock = preg_match ('| \(.*? \) | ', $ Useragent, $ matches)> 0? $ Matches [0]: '';
$ Mobile_ OS _list = array ('Google Wireless Transcoder ', 'windows CE', 'windowsce ', 'symbian', 'Android', 'armv6l', 'armv5 ', 'mobile ', 'cento', 'moyun', 'avantgo ', 'Opera Mobi', 'j2's/MIDP ', 'Smartphone', 'Go. web ', 'Palm', 'ipaq ');
$ Mobile_token_list = array ('Profile/MIDP ', 'configuration/CLDC-', '000000', '000000', '000000', '000000 ', '1970*240 ', '1970*320', 'Up. browser ', 'Up. link ', 'symbian OS', 'palmos ', 'pocketpc', 'sonyericsson', 'Nokia ', 'BlackBerry', 'Vodafone', 'benq ', 'novarra-vision', 'Iris', 'netfront', 'HTC _ ', 'xda _', 'Samsung-sgh', 'wapaka ', 'docomo ', 'iPhone ', 'iPod ');
$ Found_mobile = CheckSubstrs ($ mobile_ OS _list, $ useragent_commentsblock) | CheckSubstrs ($ mobile_token_list, $ useragent );
If ($ found_mobile ){
Return true;
} Else {
Return false;
}
}
Function CheckSubstrs ($ substrs, $ text ){
Foreach ($ substrs as $ substr ){
If (false! = Strpos ($ text, $ substr )){
Return true;
}
Return false;
}
}
If (isMobile ()){
Echo 'log on to m.phpernote.com on the mobile phone ';
} Else {
Echo 'log on to www.phpernote.com 'on your computer ';
}

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.