PHP Method for judging iPhone, iPad, Android, and PC devices-PHP source code

Source: Internet
Author: User
Because of work requirements, we need to know what kind of users have accessed our website. Now there are many mobile devices, let's take a look at the example of PHP used to judge iPhone, iPad, Android, and PC devices. Because of work requirements, we need to know what kind of users have accessed our website. Now there are many mobile devices, let's take a look at the example of PHP used to judge iPhone, iPad, Android, and PC devices.

Script ec (2); script

I set Windows devices as PCs. After all, my blog is intended for Chinese users. Most home devices still use Windows systems.

The principle is to determine the user agent submitted by the browser.

The Code is as follows:

// Obtain the USER AGENT
$ Agent = strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']);

// Analyze data
$ Is_pc = (strpos ($ agent, 'windows nt '))? True: false;
$ Is_iphone = (strpos ($ agent, 'iphone '))? True: false;
$ Is_ipad = (strpos ($ agent, 'ipad '))? True: false;
$ Is_android = (strpos ($ agent, 'android '))? True: false;

// Output data
If ($ is_pc ){
Echo "this is PC ";
}
If ($ is_iphone ){
Echo "this is the iPhone ";
}
If ($ is_ipad ){
Echo "this is iPad ";
}
If ($ is_android ){
Echo "this is Android ";
}
?>

If you only determine whether the device is an iphone device, perform the following operations:

The Code is as follows:

Function get_device_type (){
$ Agent = strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']);
$ Type = 'other ';
If (strpos ($ agent, 'iphone ') | strpos ($ agent, 'ipad ')){
$ Type = 'ios ';
}
If (strpos ($ agent, 'android ')){
$ Type = 'android ';
}
Return $ type;
}

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.