PHP simple way to Judge iphone, IPad, Android and PC devices _php tips

Source: Internet
Author: User
Tags php programming php regular expression

This article is an example of how PHP simply judges the iphone, IPad, Android, and PC devices. Share to everyone for your reference, specific as follows:

Because the work needs we need to know what kind of the user visited my site, now a lot of mobile devices, the following we look at the small compiled a section of PHP to determine the iphone, IPad, Android, PC device examples.

I will use the Windows system device as a PC, after all, blog for Chinese users, most of the home device or Windows system.

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 '));
Analysis 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 the ipad";
  }
  if ($is _android) {
    echo "this is Android";
  }
? >

If you only judge whether the iphone device can be manipulated as follows, 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;
}

More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "Php Curl Usage Summary", "PHP Socket Usage Summary", "PHP Regular Expression Usage summary", "PHP string (String) Usage Summary", " PHP array Operation techniques Encyclopedia, "PHP Mathematical Calculation Skills Summary", "PHP object-oriented Programming Introductory Course", "PHP Data structure and algorithm tutorial", "PHP Programming algorithm Summary" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP 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.