r/PHPhelp • u/fonebone819 • Apr 05 '25
Issues with detecting mobile browser
I have a site where I am detecting the browser to know if it is a mobile device. I have tried 2 different types of code, and both give mixed results. I have this code check on the main page (pages.php), and within the main page I have other include files for each page. Some of the pages load as mobile browser, and some load as a regular browser. The URL and pages are referenced as https://mysite.com/pages.php?page=X. It seems to not matter if I am actually viewing the pages on mobile or PC/Mac. For example, page=1 shows as mobile, and page=2 shows as regular, no matter if I am viewing them on mobile or regular.
The code I am using is ::
$isMob = is_numeric(strpos(strtolower($_SERVER\["HTTP_USER_AGENT"\]), "mobile"));
if($isMob) { $browserdesc = "Mobile Browser"; }
else { $browserdesc = "Regular Browser"; }`I get similar results with much larger code (not sure if I should post it here... ?).
TIA!
7
u/martinbean Apr 05 '25
Why are you trying to detect “mobile” browsers in the first place? If you want to adjust your web page for smaller screens then you should have a single, responsive design.
2
u/colshrapnel Apr 05 '25 edited Apr 05 '25
It's impossible to answer your question it its current form.
Logically, it shouldn't depend on which page is called. The only realistic explanation is that you are calling this code for page=1 and don't for page=2. Or may be some code on the page=2 modifies HTTP_USER_AGENT. Yet again, we cannot tell without seeing the code. Or you can try to debug it yourself. Do a
var_dump($isMob, $_SERVER["HTTP_USER_AGENT"]);on the page=2 and show us the result1
u/Big-Dragonfly-3700 Apr 05 '25
You may have an assignment operator (one =) instead of a comparison operator (two == or three ===) in your code that's assigning a value and testing the result of the assignment, instead of preforming a comparison. Also, are you comparing against $isMob (you should be) or $browserdesc and if $browserdesc, is the letter case correct?
1
u/identicalBadger Apr 05 '25
Do this in the front end, not the back end. Idk if bootstrap is still big, but that’s the css framework I liked the most
1
u/mtetrode Apr 05 '25
One Google search leads me to
https://detectmobilebrowsers.mobi/detect-mobile-php.php
Which quite accurately shows me what I use.
These kind of questions have been solved, first look for existing libraries instead of doing it yourself.
1
u/No_Astronomer9508 Apr 06 '25 edited Apr 06 '25
Your way will not work properly because there are useragents too, that don't have the mobile tag
in it. You should filter for operating system too. i wrote a little function you can use:
<?php function isMobile() { $ua=strtolower($_SERVER\['HTTP_USER_AGENT'\]); if( strpos($ua,'mobile') || strpos($ua,'android') || strpos($ua,'iphone') || strpos($ua,'ipad') || strpos($ua,'ipod') || strpos($ua,'blackberry') || strpos($ua,'nokia') || strpos($ua,'palmos') || strpos($ua,'samsung') || strpos($ua,'sonyericsson') || strpos($ua,'phone') || strpos($ua,'raspbian') || strpos($ua,'opr') || strpos($ua,'xiaomi') || strpos($ua,'miuibrowser') || strpos($ua,'huaweibrowser') || strpos($ua,'kfsuwi') ){$browserdesc="Mobile Browser";} elseif ( strpos($ua,'windows') || strpos($ua,'mac os') || strpos($ua,'mac os') || strpos($ua,'os/2') || strpos($ua,'symbian os') || strpos($ua,'openbsd') || strpos($ua,'freebsd') || strpos($ua,'sun os') || strpos($ua,'qnx') || strpos($ua,'beos') || strpos($ua,'netbsd') ){$browserdesc="Regular Browser";} else {$browserdesc="Unknown Browser";} return $browserdesc; } echo isMobile(); ?>
0
u/[deleted] Apr 05 '25
[deleted] — view deleted comment