How to detect mobile and tablet device using php code with mobile detect class. Using this class user easy get the version of mobile, browser of the mobile and manufacture details and etc.
detect mobile and tablet device using php
- On this technical technology, the net has now long past mobile. Internet site are made keeping in thoughts that humans may be accessing them their mobiles and tablets.
- Despite the fact that internet site are made with the css media queries which reply to the devices, it does now not gives minute info of the user devices like their version, platform, and many others.
- Using Mobile detect with php library we are able to get details in their devices like their platforms, versions, browsers and mobile manufacture company name etc. It helps in keeping track of the data for you to gain the internet site owner.
- detect mobile and tablet device using php.
What is mobile Detect class ??
- Mobile Detect is a simple PHP class for detecting mobile devices and tablets. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment and version.
- It is a core PHP library and works on server side it is not a replacement for Responsive Web Design (media queries) or other forms of client-side feature detection using detect mobile and tablet device using php.
- In this Demo file, i will show you how to detect mobile device using mobile detect library.
- First Include the Mobile_Detect.php class file, then create object of this class for access methods of this class.
<?php require 'Mobile_Detect.php'; $detect = new Mobile_Detect; $deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer'); ?>
- After creating $detect Object you will access all the method like IsMobile, IsTablet, Is Computer etc.
if( $detect->isiOS() ){ echo ("This is ios"); } if( $detect->isAndroidOS() ){ echo("This is AndroidOs"); } if( $detect->isJavaOS() ){ echo ("This is java"); } if( $detect->isWindowsPhoneOS() ){ echo ("This is WindowsPhone"); } if( $detect->isSymbianOS() ){ echo ("This is isSymbianOS"); }
- When Website open in ios mobile IsIos () Condition Become True and execute echo message.
- here i include full demo file with live demo. it’s helpful to understand clearly.
- detect mobile and tablet device using php
Demo File
<?php require 'Mobile_Detect.php'; $detect = new Mobile_Detect; $deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer'); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="keywords" content="Mobile Detect,Mobile Detect Using PHP"> <meta name="author" content="BhavinShiroya"> <title>Mobile Detect Using PHP</title> <style> * { margin: 0; } html, body { margin:0; padding:0; height:100%; } body { border: 0; font: 12px "Trebuchet MS"; color: #000; background:#fff; } a{ text-decoration: none; color:#6D37B0; } a:hover { color: #8D8D8D; } #body{ width: 1004px; margin: 0 auto; max-width: 100%; padding:20px 0 70px 0; height: 100%; } #container { min-height:100%; position:relative; } footer { position: absolute; bottom: 0; left:0; height: 70px; width: 100%; background:#f7f7f7; color:#000; } .copyright{float:left;padding:10px 0 0 20px; } .footerlogo{float:right;padding:10px 20px 0 0;} .resultRow{width:100%; margin:2px 0; border:1px solid #8D8D8D;padding:2px; color:#000;} .height10{clear:both;height:10px;} .height30{clear:both;height:30px;} .mainTitle{ font-size:35px; text-align:center;} .title{ font-size:20px; padding:10px;} .links{border:1px solid #000; padding:5px; text-decoration:none;color:#000;} .links:hover{text-decoration:underline;color:#fff; background:#6D37B0;} .selected{border:1px solid #000; padding:5px; text-decoration:none;color:#fff; background:#6D37B0;} /****************************************************/ table { *border-collapse: collapse; /* IE7 and lower */ border-spacing: 0; width: 100%; } .bordered { font-size:14px; border: solid #ccc 1px; -webkit-box-shadow: 0 1px 1px #ccc; -moz-box-shadow: 0 1px 1px #ccc; box-shadow: 0 1px 1px #ccc; } .bordered td, .bordered th { padding: 5px; border-bottom: 1px solid #403737; } .bordered th { background-color: #E75F63; border-top: none; } .bordered tbody tr:nth-child(even) { background: #f5f5f5; border:1px solid #000; } .true{background: blue; color: #FFF;} </style> </head> <body> <div id="container"> <div id="body"> <div class="mainTitle" >Mobile Detect Using PHP</div> <div class="height20"> </div> <div class="title" style="text-align: center;">Your device is <span style="text-decoration: underline; font-style: italic;"><?php echo $deviceType; ?></div> <div class="height20"></div> <article> <div class="height10"> </div> <table cellspacing="0" cellpadding="0" class="bordered" style="width: 100%;"> <tbody> <tr> <th colspan="2" style="text-align: left; color: #FFFFFF;">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" style="text-align: left; color: #FFFFFF;">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" style="text-align: left; color: #FFFFFF;">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" style="text-align: left; color: #FFFFFF;">Other tests</th> </tr> <tr> <td>isiphone()</td> <td><?php var_dump($detect->isiphone()); ?></td> </tr> <tr> <td>isIphone()</td> <td><?php var_dump($detect->isIphone()); ?></td> </tr> <tr> <td>istablet()</td> <td><?php var_dump($detect->istablet()); ?></td> </tr> <tr> <td>isIOS()</td> <td><?php var_dump($detect->isIOS()); ?></td> </tr> <tr> <td>isWhateverYouWant()</td> <td class="randomcrap"><?php var_dump($detect->isWhateverYouWant()); ?></td> </tr> </tbody> </table> </article> <div class="height30"></div> <footer> <div class="copyright"> © 2014 - <?php echo date('Y') ?> <a href="http://www.techsofttutorials.com/" target="_blank">TechsoftTutorials</a>. All rights reserved </div> <div class="footerlogo"><a href="http://www.techsofttutorials.com/" target="_blank"><img src="http://techsofttutorials.com/wp-content/uploads/2014/12/new2.png" width="200" height="47" alt="TechsoftTutorials Logo" /></a> </div> </footer> </div> </div> </body> </html>