1

I am using http://mobiledetect.net/ to show something only on Desktop. for that I am using this code:

<?php

include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

// Some Code Here

if ( !$detect -> isMobile() && !$detect -> isTablet() && !$detect -> isiOS() && !$detect -> isAndroidOS() ) {
  echo 'SomeThing';
}

?>

The problem is that when I'm using desktop everything is fine and I get the text. But on mobile, the first time I visit the page I SEE the text and then when I reload it, I don't. How can I fix this problem?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    best to use CSS: https://stackoverflow.com/questions/16550485/hide-div-tag-on-mobile-view-only – Alive to die - Anant Feb 17 '21 at 13:04
  • @Alive to Die It has to be php – Elyas Behroozizade Feb 17 '21 at 13:05
  • @Alive to Die It's +1400 lines code. I shared the link! – Elyas Behroozizade Feb 17 '21 at 13:07
  • 1
    `It has to be php`....because? Most websites use responsive design techniques involving CSS and HTML. That's the way to do it, because the code can then react to the real screen dimensions, rather than (presumably) trying to second-guess them based on the minimal information the server receives, such as a user-agent string or something (which is easily spoofed, and new devices/OS versions/browsers can often change them. There's no standard, it's pretty arbitrary and not massively reliable. And can't react to real-time changes in size (e.g. user moves phone horizontal). – ADyson Feb 17 '21 at 13:08
  • `then use the conditions in same way what it given in the shared link` and How is that? – Elyas Behroozizade Feb 17 '21 at 13:10
  • 1
    *"But on mobile, the first time I visit the page I SEE the text and then when I reload it, I don't."* - Time to do some debugging then. What is the difference between the initial page load and the subsequent page reloads? Has the user agent header changed? Is the server-side logic different in some way? Using a step debugger on mobile devices may be prohibitively difficult, but you can at least log some useful output to see what's going on. Which specific condition is failing and how is it failing? If you *must* do this server-side then you need to debug your server-side code. – David Feb 17 '21 at 13:12
  • @ElyasBehroozizade I am being superstious, try this: `$isMobile = false; if ( $detect->isMobile() || $detect->isTablet() || $detect->isiOS() || $detect->isAndroidOS()) { $isMobile = true; } if($isMobile === false){ echo "something"; }` – Alive to die - Anant Feb 17 '21 at 13:18
  • @ElyasBehroozizade one more thing : are you using chrome inspector toggle device toolbar for checking mobile device and desktop version? http://prntscr.com/zuv9st – Alive to die - Anant Feb 17 '21 at 13:21
  • also.. you should simplify the if statement just using $detect->isMobile(), because the next conditions are redundants, you must use them just for exclusion proposes (like $detect->isMobile() && ! $detect->isTablet(), to detect only phone mobiles) – Rafael Dourado D Feb 17 '21 at 13:29

0 Answers0