PHP's approach to judging iphone, IPad, Android, and PC devices

Source: Internet
Author: User

Because the job needs us to know what it is. The user visited my website, now there are many kinds of mobile devices, let's look at the small compilation of a section of PHP to determine the iphone, IPad, Android, PC device examples.

Note: This code of the PC system for Windows

The principle is to determine the user AGENT submitted by the browser

<?php

Get the user AGENT

$agent = Strtolower ($_server[' http_user_agent ');

Make judgments

$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;

if ($is _pc)

{

echo "This is PC";

}

if ($is _iphone)

{

echo "This is the iphone";

}

if ($is _ipad)

{

echo "This is the ipad";

}

if ($is _android)

{

echo "This is Android";

}

If you only determine whether the iphone device can be operated as follows, the code is as follows:

<?php

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;

}

?>

PHP's approach to judging iphone, IPad, Android, and PC devices

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.