How to detect between mobile & tablets in PHP
-
26-10-2019 - |
Question
I read on this thread: Simplest way to detect a mobile device on how to know if the browser is a mobile device. the general code is this:
<?php include("Mobile_Detect.php"); include("demoData.php");
$detectIsMobile = new Mobile_Detect(); ?>
The problem is that I want to treat tablets (iPad & xoom).
I saw there that there is a isIpad() function that I have tested yet - but that still doesn't solve the difference between tablets and mobile phones.
Any idea?
thanks, Alon
Solution
Use
<?php
if($detect->isTablet()){
// any tablet
}
?>
You can refer this page for more info http://code.google.com/p/php-mobile-detect/
OTHER TIPS
The only way to do this is with a huge lookup table of User-Agent:
strings.
get_browser()
would probably be able to do what you want, but you would need to make sure that you keep the browscap file very up to date - new tablet models are being released on a weekly basis.
Alternatively there may some Javascript way to do it (although I don't know what that might be) but
- you would still have to keep a very large lookup table updated
- you should never rely on Javascript for any kind of functionality.
$detect = new Mobile_Detect;
$deviceType = ( $detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
echo $deviceType;