Consider the UserAgent determination.

Is the accessing device a desktop? Is it a tablet? Is it a smartphone?
The user agent determines this.

目次

What is UserAgent?

A string of characters that a web browser or other client software uses to communicate information about itself to the server when accessing the server. This string contains information about the type of browser you are using, its version, operating system (OS) and device.

A string of this form is passed to the web server so that it can make decisions.

Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

Determine the mobile device.

The first step is to determine whether the device is a mobile device.
This is very simple.

As the MDN notes, the decision can be made based on whether the user agent string contains Mobi.

https://developer.mozilla.org/ja/docs/Web/HTTP/Browser_detection_using_the_user_agent

要するに、モバイル端末を検出するには、ユーザーエージェント文字列のどこかに文字列 Mobi があるかどうかを探すことをお勧めします。

Judging iPads, Android tablets

The Android tablet decision can be made by looking at whether the user agent contains ‘android’ and ‘mobile’. When these two strings are present, it is an Andorid mobile device, otherwise it is an Android tablet device.

https://developers.google.com/search/blog/2011/03/mo-better-to-also-detect-mobile-user?hl=ja

The iPad, on the other hand, is simple and can be determined by whether the user agent contains ‘ipad’. If it is included, it is an iPad tablet.

However, the iPad has a ‘Show desktop websites’ function. Note that the user agent for users who have this switched on will switch from ‘ipad’ to ‘macintosh’.

Judging the desktop

Considering the above two patterns, it can be concluded that access is from the desktop at other times.

Summary

A very simple conditional expression can be written to determine whether the device is a mobile device or not.

However, access from hardware such as the Nintendo Switch does not include Mobi strings, so this needs to be investigated separately and considered for handling.

よかったらシェアしてね!
目次