PHP to determine the function of a mobile device ismobile ()

Source: Internet
Author: User
Tags foreach lenovo

No nonsense on the code, the use of the method is

The code is as follows Copy Code


<?php
if (IsMobile ()) {}
if (!ismobile ()) {}
?>


function IsMobile () {

$user _agent = $_server[' http_user_agent '];
$mobile _agents = Array ("240x320", "Acer", "Acoon", "acs-", "Abacho", "Ahong", "Airness", "Alcatel", "Amoi", "Android", "a Nywhereyougo.com "," applewebkit/525 "," applewebkit/532 "," Asus "," Audio "," Au-mic "," Avantogo "," Becker "," BenQ "," Bilbo "," Bird "," BlackBerry "," blazer "," Bleu "," cdm-"," Compal "," Coolpad "," Danger "," Dbtel "," Dopod "," Elaine "," Eric ", "Etouch", "Fly", "Fly_", "fly-", "Go.web", "goodaccess", "Gradiente", "Grundig", "Haier", "Hedy", "Hitachi", "HTC", "Hua Wei "," Hutchison "," Inno "," ipad "," iPAQ "," ipod "," Jbrowser "," KDDI "," kgt "," KWC "," Lenovo "," LG "," Lg2 "," Lg3 "," LG4 ", "Lg5", "Lg7", "Lg8", "Lg9", "lg-", "lge-", "Lge9", "Longcos", "Maemo", "Mercator", "Meridian", "Micromax", "MIDP", "Mini" , "Mitsu", "MMM", "MMP", "Mobi", "mot-", "Moto", "nec-", "NetFront", "Newgen", "Nexian", "Nf-browser", "Nintendo", "Nitro" , "Nokia", "Nook", "Novarra", "Obigo", "Palm", "Panasonic", "Pantech", "Philips", "Phone", "pg-", "PlayStation", "pocket", "Pt-", "qc-", "Qtek", "Rover", "Sagem", "sama", "Samu", "Sanyo", "Samsung", "sch-", "Scooter", "sec-", "Sendo", "sgh-", "Sharp", "Siemens", "sie-", "Sof Tbank "," Sony "," Spice "," Sprint "," SPV "," Symbian "," tablet "," Talkabout "," tcl-"," Teleca "," Telit "," Tianyu "," tim-"," Toshiba "," TSM "," Up.browser "," Utec "," Utstar "," Verykool "," Virgin "," vk-"," Voda "," Voxtel "," VX "," WAP "," Wellco "," WI G Browser "," Wii "," Windows CE "," wireless "," XDA "," Xde "," ZTE ");

$is _mobile = false;
foreach ($mobile _agents as $device) {
if (Stristr ($user _agent, $device)) {
$is _mobile = true;
Break
}
}
return $is _mobile;
}

Netizens have added a

The code is as follows Copy Code

<?php
function IsMobile ()
{
If you have http_x_wap_profile, you must be a mobile device.
if (Isset ($_server[' http_x_wap_profile '))
{
return true;
}
If the VIA information contains WAP it must be a mobile device, and some service providers block the information
if (Isset ($_server[' Http_via '))
{
Not found for flase, otherwise true
Return Stristr ($_server[' Http_via '], "WAP")? True:false;
}
Brain residue method, to judge the mobile phone to send the client logo, compatibility needs to be improved
if (Isset ($_server[' http_user_agent '))
{
$clientkeywords = Array (' Nokia '),
' Sony ',
' Ericsson ',
' MOT ',
' Samsung ',
' HTC ',
' SGH ',
' LG ',
' Sharp ',
' Sie-',
' Philips ',
' Panasonic ',
' Alcatel ',
' Lenovo ',
' iphone ',
' ipod ',
' BlackBerry ',
' Meizu ',
' Android ',
' NetFront ',
' Symbian ',
' UCWeb ',
' WindowsCE ',
' Palm ',
' Operamini ',
' Operamobi ',
' Openwave ',
' Nexusone ',
' CLDC ',
' MIDP ',
' WAP ',
' Mobile '
);
Find keywords for mobile browsers from Http_user_agent
if (Preg_match) ("/". Implode (' | ', $clientkeywords). ")/I", Strtolower ($_server[' http_user_agent ')))
{
return true;
}
}
Agreement law because there may be inaccurate and put it to the final Judgment
if (Isset ($_server[' http_accept '))
{
If only WML is supported and HTML is not supported it must be a mobile device
If WML and HTML are supported but WML is a mobile device before HTML
if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false) && (Strpos ($_server[' http_accept '), ' text/ HTML ') = = False | | (Strpos ($_server[' http_accept '], ' vnd.wap.wml ') < Strpos ($_server[' http_accept '], ' text/html ')))
{
return true;
}
}
return false;
}
?>

Foreign people like to write class, there is a mobile detect

Mobile_detect Simple Use instance

The code is as follows Copy Code

include ' Mobile_detect.php ';
$detect = new Mobile_detect ();
 
//Check for all mobile device.
if ($detect->ismobile ())
 
//check for any TABLET.
if ($detect->istablet ())
 
//Check for any mobile device, excluding tablets.
if ($detect->ismobil E () && $detect->istablet ())
 
if ($detect->ismobile () &&! $detect->istablet ())
 
//alternative to $detect->isandroidos ()
$detect->is (' Androidos ');
 
//Batch Usage
foreach ($userAgents as $userAgent) {
  $detect->setuseragent ($userAgent);
  $isMobile = $ Detect->ismobile ();
}
 
//Version check.
$detect->version (' IPad ');//4.3 (float)

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.