How to determine whether the user is browsing in the app or on the web?

A common way to determine whether a user is browsing in a mobile app or on the web is to check the request's User-Agent string.

The user-agent string is an HTTP header sent by a web browser or other client program that describes the type and version of the requesting client. For example, a user agent string may contain information such as browser name, version, operating system, and more.

Here are several examples of possible user agent strings:

  • A typical desktop browser user agent string might look like this:

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537
    
  • A typical mobile browser user-agent string might look like this:

    Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1
    
  • A request from a mobile app may have a completely different user-agent string, usually depending on how the mobile app has set it up. For example, an application that uses the Android system's WebView component might have the following user agent:

    Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19
    

Note that the user agent string can be modified by the client, so it cannot provide 100% accuracy. Additionally, there are many different browsers and devices, and the format and content of the user-agent string can vary widely.

If your mobile application uses a built-in web view (such as Android's WebView or iOS's WKWebView) to display web content, you can probably configure the web view's user agent string to contain a specific tag, and then you Your server can check this flag to make sure the request is from your mobile app.

Another strategy is to include a specific HTTP header or parameter with every request your application makes that indicates that the request is coming from your application. X-App-VersionFor example, you can include a HTTP header with every request to your application . This method is more reliable than checking the user agent string, but requires you to control the application code.

Guess you like

Origin blog.csdn.net/m0_57236802/article/details/131636177