Thinkphp determines whether a visitor is a mobile phone or PC visitor. Thinkphp judges the visitor as a mobile phone or PC. thinkphp visitor this article describes how thinkphp judges the visitor as a mobile phone or PC. Share it with you for your reference. Thinkphp determines whether a visitor is a mobile phone or PC visitor.
This example describes how thinkphp judges a visitor as a mobile phone or PC. Share it with you for your reference. The specific implementation method is as follows:
I. problems:
Recently, I am going to make a small upgrade for my website so that users can display templates suitable for display on the mobile phone terminal when scanning the QR code on their mobile phone [ThinkPHP3.0 is used]. the code is for reference by others.
II. implementation method:
Here is a simple two-step process:
The general version number, the browser version number, and the version number (only mobile terminal information is contained in the array in the pasted code, so you only need to judge whether it is a value in the array ).
Determine whether a visitor is a mobile phone, pad, or other mobile terminal based on the value in the array. If yes, specify the Project path and name as your mobile phone template, as shown in figure
The code is as follows:
Define ('app _ name', 'mobi ');
Define ('app _ path', './mobi /');
Add comments as much as possible for the following code:
The code is as follows:
// Determine whether the mobile phone belongs
// The code looks a lot. In fact, the array looks a lot messy and should not be intimidated by the surface phenomenon!
Function is_mobile (){
$ User_agent = $ _ SERVER ['http _ USER_AGENT '];
$ Mobile_agents = Array ("240x320", "acer", "ACO on", "acs-", "abacho", "ahong", "airness", "alcatel ", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic ", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm -", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly _", "fly-", "go. web "," goodaccess "," gradiente "," grundig "," haier "," hedy "," hitachi "," htc "," huawei "," Huchison ", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge -", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm ", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser ", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips ", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem ", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh -", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian ", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshba", "tsm ", "up. browser "," utec "," utstar "," verykool "," virgin "," vk-"," voda "," voxtel "," vx "," wap ", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte ");
$ Is_mobile = false;
Foreach ($ mobile_agents as $ device) {// Here, the value is traversed to find whether the above string has appeared
If (stristr ($ user_agent, $ device) {// stristr to check whether the visitor information is in the preceding array. if the information does not exist, it is PC.
$ Is_mobile = true;
Break;
}
}
Return $ is_mobile;
}
Define ('think _ path', './CORE /');
If (is_mobile () {// jump to the wap Group
Echo 'you have accessed the mobile phone and jumped to the mobile phone end ';
Define ('app _ name', 'mobi ');
Define ('app _ path', './mobi /');
} Else {
Echo 'You accessed from the PC ';
Define ('app _ name', 'home ');
Define ('app _ path', './Home /');
}
Define ('app _ debug', false );
Require THINK_PATH. 'core. php ';
I hope this article will help you with ThinkPHP framework programming.
Examples in this article describe how thinkphp judges a visitor as a mobile phone or PC. Share it with you for your reference. Details...