PHP: iPhone, iPad, Android, and PC devices

Source: Internet
Author: User

PHP: iPhone, iPad, Android, and PC devices

This article describes how PHP can easily determine iPhone, iPad, Android, and PC devices. We will share this with you for your reference. The details are as follows:

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.

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

The principle is to determine the user agent submitted by the browser. The Code is as follows:

<? Php // get 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 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:

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.