PHP Mobile-Detect检查手机移动设备访问网站
这里给大家介绍一个移动设备检查类了,从google找到的mobile-detect类,这个类可以ct判断android、ios、Tablet平板,浏览器,等比较完美完善的判断ipad,iPhone,win8等
使用方法
<?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. } ?>
官方实例
<?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" 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</h1> <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><?php echo var_dump($detect->isWhateverYouWant()); ?></td> </tr> </tbody> </table> </section> </body> </html>
php-mobile-detect文件下载地焉: http://www.phprm.com
本文地址:http://www.phprm.com/code/53668.html
转载随意,但请附上文章地址:-)