Below is the link will help to detect Mobile or Tablet.
PHP user agent determining for both mobile and tablet
-
29-09-2022 - |
Question
i use this:
$isMobile = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Mobile');
if ($isMobile) {
$Ymobile = "block";
} else {
$Ymobile = "none";
}
This determines if the user is on mobile device, if yes it sets the class of a div to block.
However for a tablet, the div is shown.
So i changed the 'Mobile' to 'Tablet' and it worked, the div didnt show on the tablet.
How come the else doesnt hide the div on a tablet?
I thought i could do e.g.:
$isMobile = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Mobile');
$isTablet = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Tablet');
if ($isMobile) {
$Ymobile = "block";
} else if ($isTablet) {
$Ymobile = "none";
} else {
$Ymobile = "none";
}
But that still shows the div.
Where am i going wrong?
Solution
OTHER TIPS
Just an idea..
Why don't you try a switch statement. If no 'Mobile' is detected then set none, in all other cases (is Mobile) then go for "block"
switch(strpos($_SERVER['HTTP_USER_AGENT'],'Mobile')) {
case false:
$Ymobile = "none";
break;
default:
$Ymobile = "block";
break;
}
Ended up using this:
https://code.google.com/p/php-mobile-detect/wiki/Mobile_Detect
Very simple to use with tons of options.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow