PHP Mobile-Detect: check Mobile devices for website access

Source: Internet
Author: User
Tags foreach php foreach

Usage

The code is as follows: Copy code

<? Php
Include 'mobile _ Detect. Php ';
$ Detect = new Mobile_Detect ();


If ($ detect-> isMobile ()){
// Any mobile device.
    }

If ($ detect-> isTablet ()){
// Any tablet device.
    }
Check for a specific platform:

If ($ detect-> isiOS ()){
// Code to run for the Apple's iOS platform.
    }

If ($ detect-> isAndroidOS ()){
// Code to run for the Google's Android platform.
    }

Official instances

The code is as follows: Copy code

<? Php


Require_once 'mobile _ Detect. Php ';
$ Detect = new Mobile_Detect;
$ DeviceType = ($ detect-> isMobile ()? ($ Detect-> isTablet ()? 'Tablet': 'phone'): 'computer ');
$ ScriptVersion = $ detect-> getScriptVersion ();

?> <! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml" lang = "en" xml: lang = "en">
<Head>
<Meta charset = "utf-8">
<Meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, minimum-scale = 1.0, user-scalable = no ">
<Title> Mobile Detect Local Demo </title>
<Style type = "text/css">
Html {font-size: 100%;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100% ;}
Body {margin: 0; padding: 0 1em; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1em; color: #333333; background-color: # ffffff; max-width: 320px ;}
Body, td {font-size: 1em ;}
Table th {text-align: left ;}
A {color: # 0088cc; text-decoration: underline ;}
A: hover {color: #005580; text-decoration: underline ;}
Header h1 small {font-size: small ;}
Section {margin-bottom: 2em ;}
Section h1 {font-size: 100% ;}
. InfoText {font-size: 85% ;}
. Response {color: red ;}
. Computer {background-color: blue; color: white ;}
. Tablet {background-color: yellow; color: black ;}
. Phone,. true {background-color: green; color: white ;}
. SendDataButton {border-radius: 1em;-moz-border-radius: 1em;-webkit-border-radius: 1em; padding: 0.5em 1em; cursor: pointer ;}
. SendDataButton_yes {
Color: white;
Border: 1px solid # 56A00E;
Background: #74B042;
Font-weight: bold;
Color: # ffffff;
Text-shadow: 0 1px 0 #335413;
Background-image:-webkit-gradient (linear, left top, left bottom, from (#74B042), to (# 56A00E);/* Saf4 +, Chrome */
Background-image:-webkit-linear-gradient (#74B042, # 56A00E);/* Chrome 10 +, Saf5.1 + */
Background-image:-moz-linear-gradient (#74B042, # 56A00E);/* FF3.6 */
Background-image:-ms-linear-gradient (#74B042, # 56A00E);/* IE10 */
Background-image:-o-linear-gradient (#74B042, # 56A00E);/* Opera 11.10 + */
Background-image: linear-gradient (#74B042, # 56A00E );
        }
. SendDataButton_no {
Color: white;
Border: 1px solid # cd2c24;
Background: red;
Font-weight: bold;
Color: # ffffff;
Text-shadow: 0 1px 0 #444444;
Background-image:-webkit-gradient (linear, left top, left bottom, from (# e13027), to (# b82720);/* Saf4 +, Chrome */
Background-image:-webkit-linear-gradient (# e13027, # b82720);/* Chrome 10 +, Saf5.1 + */
Background-image:-moz-linear-gradient (# e13027, # b82720);/* FF3.6 */
Background-image:-ms-linear-gradient (# e13027, # b82720);/* IE10 */
Background-image:-o-linear-gradient (# e13027, # b82720);/* Opera 11.10 + */
Background-image: linear-gradient (# e13027, # b82720 );
        }
# FeedbackForm fieldset {border: 1px dotted #333 ;}
# FeedbackForm label {font-weight: bold; font-size: 85% ;}
# FeedbackForm textarea {width: 260px ;}
</Style>
  


</Head>
<Body>


<Section>

<P> This is a <B> <? Php echo $ deviceType;?> </B>. Your UA is <B class = "<? Php echo $ deviceType;?> "> <? Php echo htmlentities ($ _ SERVER ['http _ USER_AGENT ']);?> </B> </p>

   


</Section>


<! -- Copy to GitHub demo. php -->
<Section>
<H1> Supported methods <Table cellspacing = "0" cellpadding = "0">
<Tbody>
<Tr>
<Th colspan = "2"> Basic detection methods </th>
</Tr>
<Tr>
<Td> isMobile () </td> <td <? Php $ check = $ detect-> isMobile (); if ($ check):?> Class = "true" <? Php endif;?> <? Php var_dump ($ check);?> </Td>
</Tr>
<Tr>
<Td> isTablet () </td> <td <? Php $ check = $ detect-> isTablet (); if ($ check):?> Class = "true" <? Php endif;?> <? Php var_dump ($ check);?> </Td>
</Tr>
</Tbody>
<Tbody>
<Tr>
<Th colspan = "2"> Custom detection methods </th>
</Tr>
<? Php foreach ($ detect-> getRules () as $ name => $ regex ):
$ Check = $ detect-> {'is '. $ name }();
?>
<Tr>
<Td> is <? Php echo $ name;?> () </Td>
<Td <? Php if ($ check):?> Class = "true" <? Php endif;?> <? Php var_dump ($ check);?> </Td>
</Tr>
<? Php endforeach;?>
</Tbody>
<Tbody>
<Tr>
<Th colspan = "2"> Experimental version () method </th>
</Tr>
<? Php
Foreach ($ detect-> getProperties () as $ name => $ match ):
$ Check = $ detect-> version ($ name );
If ($ check! = False ):
?>
<Tr>
<Td> version (<? Php echo $ name;?>) </Td>
<Td> <? Php var_dump ($ check);?> </Td>
</Tr>
<? Php endif;?>
<? Php endforeach;?>
</Tbody>
<Tbody>
<Tr>
<Th colspan = "2"> Other tests </th>
</Tr>
<Tr>
<Td> isiphone () </td>
<Td> <? Php echo var_dump ($ detect-> isiphone ();?> </Td>
</Tr>
<Tr>
<Td> isIphone () </td>
<Td> <? Php echo var_dump ($ detect-> isIphone ();?> </Td>
</Tr>
<Tr>
<Td> istablet () </td>
<Td> <? Php echo var_dump ($ detect-> istablet ();?> </Td>
</Tr>
<Tr>
<Td> isIOS () </td>
<Td> <? Php echo var_dump ($ detect-> isIOS ();?> </Td>
</Tr>
<Tr>
<Td> isWhateverYouWant () </td>
<Td class = "randomcrap"> <? Php echo var_dump ($ detect-> isWhateverYouWant ();?> </Td>
</Tr>
</Tbody>
</Table>

</Section>

</Body>
</Html>


Php-mobile-detect download location: http://file.111cn.net/upload/2013/10/M.rar

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.