Thinkphp the method _php the visitor to the handset side or the PC end instance

Source: Internet
Author: User

This article is an example of how thinkphp judge a visitor as a mobile or PC-side. Share to everyone for your reference. The implementation methods are as follows:

First, the question:

Recently ready to do a small upgrade of their own website, so that users in the mobile phone two-dimensional code scan display for the phone to show the template "I use is ThinkPHP3.0", the code is reference to others

Second, the realization method:

Here first the general idea of a simple two-step:

The version number and the browser and its version number (the posted code has only the moving end information in the array, so you only need to determine whether one of the values in the group is available).

According to the values in the array to determine whether the visitors are mobile phones, pad, and so on the mobile end, if yes, specify the project path, name for your mobile phone template version such as

Copy Code code as follows:
Define (' App_name ', ' mobi ');

Define (' App_path ', './mobi/');

The following code I try to add a comment:

Copy Code code as follows:
Determine whether it belongs to a mobile phone
The code looks a lot, in fact, is the array inside appear many and disorderly, do not be intimidated by the surface phenomenon Oh!
function Is_mobile () {
$user _agent = $_server[' http_user_agent '];

    $mobile _agents = Array ("240x320", "Acer", "Acoon", "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 "," Hutchison "," Inno "," 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-", "Toshiba", "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 goes through the values to find out if any of the above strings appear
if (Stristr ($user _agent, $device)) {//stristr Find whether the guest end information is in the array above, not the PC side.
$is _mobile = true;
Break
}
}
return $is _mobile;
}
Define (' Think_path ', './core/');
if (Is_mobile ()) {//jump to WAP grouping
Echo ' You are the mobile side of the access, has jumped to the phone end ';
Define (' App_name ', ' mobi ');
Define (' App_path ', './mobi/');
}else{
Echo ' You are a PC-side access ';
Define (' app_name ', ' home ');
Define (' App_path ', './home/');
}
Define (' App_debug ', false);
Require Think_path. ' core.php ';

I hope this article will be helpful to everyone's thinkphp framework program design.

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.