Forum Moderators: phranque

Message Too Old, No Replies

User agents for mobile

         

csdude55

7:38 pm on Feb 19, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is really a dated concept, but I'm trying to apply a temporary band-aid to something I wrote in 2013. My new build uses CSS, but the old (current) build checks the user agent in PHP to determine which features to show. It's too much work to rebuild all of it when I'm already working on a totally new rebuild anyway, so for now I just need to make sure that it's still (mostly) catching mobile devices.

Right now I check $_SERVER['HTTP_USER_AGENT'] for any of these. Can you guys and gals suggest any that could be safely removed, or any that I should add?

$mobileArr = [
'iphone',
'ipod',
'android',
'mobile',
'sony',
'symbian',
'nokia',
'samsung',
'windowsce',
'epoc',
'operamini',
'nitro',
'j2me',
'midp-',
'cldc-',
'netfront',
'mot',
'up.browser',
'up.link',
'audiovox',
'blackberry',
'ericsson,',
'panasonic',
'philips',
'sanyo',
'sharp',
'sie-',
'portalmmm',
'blazer',
'avantgo',
'danger',
'palm',
'series60',
'palmsource',
'pocketpc',
'smartphone',
'rover',
'ipaq',
'au-mic,',
'alcatel',
'ericy',
'up.link',
'vodafone/',
'wap1.',
'wap2.'
];

ClosedForLunch

10:34 pm on Feb 19, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



Just check for the word 'Mobile' in the UA and you'll catch almost all of the mobile devices being used in 2021.

You may want to consider defaulting to a mobile feature-set anyway (to catch any legacy mobile devices) if you can't positively identify a device as being a tablet or desktop.

lucy24

11:01 pm on Feb 19, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Who is your target audience? Do many of them own ancient phones?

These days I don't even bother checking for anything but
iPhone|Android
in the rare case where responsive isn't enough. (I think I currently have ONE rule, for one narrowly constrained situation, that does this.)

If you need to nab tablets as well as smartphones, that gets cut back to iP and add whatever the Windows tablet currently calls itself.

:: detour to raw logs ::

Oh yes, there's also Opera Mini. It tends not to say what kind of device it's on.

:: wandering off to see if I need to expand my rule ::

csdude55

2:02 am on Feb 20, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My original goal here was to show rich HTML buttons and contenteditable to desktop, and a simple textarea to mobile. At the time, none of the mobile devices supported contenteditable :-/

Now I'm not even sure if it's worth it now, it looks like the only current mobile browsers that don't support it is Opera Mini:

[caniuse.com...]

I dunno, I think I'm going to sort through my entire site and make sure I'm not using it anywhere else. If not, maybe I'll just delete it entirely...

@lucy24, I also had these in my original script:

(isset($_SERVER['HTTP_ACCEPT']) && strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') !== false)
(isset($_SERVER['HTTP_X_OPERAMINI_PHONE']) && $_SERVER['HTTP_X_OPERAMINI_PHONE'] !== '')

I have no clue what the first one is for, but the second appears to test for an older version of Opera Mini.

I downloaded Opera Mini to double check, though, that that value definitely doesn't exist anymore. All I see now is in the user agent, it includes:

Mobile Safari/537.36 OPR/53.x.x.x.x

So maybe checking the user agent for "mobile" and "OPR" is all I really need, too...

phranque

3:18 am on Feb 20, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



have you done a web server access log file analysis to see what set of UAs are typically visiting your site?

csdude55

5:50 am on Feb 20, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not really, and I'm not sure how... other than looking at Analytics. I looked at the general browser list, though, and I THINK I won't have any problems with them now (famous last words, I know).

I'm not sure if Opera is going to be the same as Opera Mini or if it's just the desktop version. But since Mini doesn't recognize contenteditable then I guess I still have to make allowances for it. And I'm not sure about Playstation or YaBrowser, but I've had 1 user from both in the last week so I'm really not sure if it's even worth worrying about.

Just a guess, is "Android Webview" the "show simplified view" option on Android?

Per Analytics, my visitors for the last week:

Chrome (45.59%)
Safari (41.57%)
Edge (4.71%)
Firefox (2.44%)
Samsung Internet (2.29%)
Internet Explorer (1.31%)
Amazon Silk (0.76%)
Mozilla Compatible Agent (0.58%)
Safari (in-app) (0.32%)
Android Webview (0.31%)
Opera (0.07%)
(not set) (0.01%)
UC Browser (0.01%)
Playstation 4 (0.00%)
YaBrowser (0.01%)

csdude55

9:36 pm on Feb 21, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@lucy24, as far as I can tell, Opera Mini on iOS is going to include something like this:

Mozilla/5.0 (iPad; CPU OS 11_0_3 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) OPiOS/16.0.3.120766 Mobile/15A432 Safari/9537.53

[developers.whatismybrowser.com...]

They also list these, but neither "OPiOS" nor "Mobile Safari/537.36 OPR" are on the list:

[developers.whatismybrowser.com...]

Without going through the full list of 427 thousand potential matches, I'm just running on the theory that I can search for "Opera Mini", "OPiOS" followed by "Mobile", and "Mobile Safari" followed by "OPR". That's 1 strpos() followed by 2 preg_match() in PHP, though, so kinda slow.

Unless you think the 537.36 is relatively standard, so I could save a little processing time by changing one of the preg_match() to strpos():

$ua = $_SERVER['HTTP_USER_AGENT'];

if (
strpos($ua, 'Mobile Safari/537.36 OPR') !== false ||
strpos($ua, 'Opera Mini') !== false ||
preg_match('/OPiOS\/[0-9.]+ Mobile/\w+ Safari/', $ua)
) {
// do Opera Mini stuff
}


OR, I could do it in Apache and set an [E]:

RewriteCond %{HTTP_USER_AGENT} Mobile Safari/[0-9.]+ OPR [OR]
RewriteCond %{HTTP_USER_AGENT} Opera Mini [OR]
RewriteCond %{HTTP_USER_AGENT} OPiOS/[0-9.]+ Mobile/\w+ Safari
RewriteRule ^ - [E=opera_mini:true]

lucy24

5:36 pm on Feb 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[0-9.]+
Since there is no capture and no closing anchor, all you need in this location is
[0-9]
or
\d
If you did need the entire version number, it should probably be expressed as
[0-9][0-9.]+
or
\d[\d.]+
because a leading . has got to be bogus. As always, the choice between [0-9] and \d is purely a matter of personal coding style.

I re-checked “Opera Mini” in logs. I get a lot of this kind of thing (picking one at random):
"Opera/9.80 (MAUI Runtime; Opera Mini/4.4.33576/179.156; U; en) Presto/2.12.423 Version/12.16"
with no indication of OS, such as Android or iPthingy. And there are a surprising lot of them, to all appearances human.

csdude55

6:57 pm on Feb 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As always, the choice between [0-9] and \d is purely a matter of personal coding style.

For me it's generally easier to recognize [0-9] than \d, but for the last year or so I've gotten in the habit of using \regex in PHP when I can to minimize the size of the file. In Apache, though, I use [regex] for readability.

I had read a few years ago that [regex] was faster to process, but my benchmarks in PHP have shown them to be identical. So I guess it really is just personal preference :-)

I re-checked “Opera Mini” in logs. I get a lot of this kind of thing (picking one at random):

I had never seen Presto before, but apparently that's exclusively an Opera Mobile / Opera Mini thing:

[deviceatlas.com...]

If it matters to you, then I'm relatively sure that it will be Android since it doesn't include iWhatever. But Opera seems to be big on secrecy, so I wouldn't be surprised for it to change that drastically in the near future.