Home >Backend Development >PHP Tutorial >PHP determines whether the webpage is loaded by Android webview
一. Idea:
1. Android uses webview to load web pages, and sets its userAgent specific characters on the android side
2. Server-side PHP determines whether the obtained user agent contains specific characters given by Android
3. If there is, load the webpage for Android webview
II. Code
Android side requires settings:
(These two lines of code: Find the userAgent on the Android side, change the Android string to APP_WEBVIEW Android, APP_WEBVIEW is the sign for php)
<code>String userAgent = webView<span>.getSettings</span>()<span>.getUserAgentString</span>()<span>;//找到webview的useragent</span> webView<span>.getSettings</span>()<span>.setUserAgentString</span>(userAgent<span>.replace</span>(<span>"Android"</span>, <span>"APP_WEBVIEW Android"</span>))<span>;//在useragent上添加APP_WEBVIEW 标识符,服务器会获取该标识符进行判断</span></code>
php server side:
<code><span><span><?php</span><span>if</span>(strpos(<span>$_SERVER</span>[<span>'HTTP_USER_AGENT'</span>],<span>'APP_WEBVIEW'</span>) !== <span>false</span>){ <span>echo</span><span>"<script>alert('当前网页是Android webview 加载');</script>"</span>; }<span>else</span>{ <span>echo</span><span>"<script>alert('当前网页不是Android webview 加载');</script>"</span>; } <span>?></span></span></code>
Three. Description
If it is iOS, the code judgment on the PHP side is the same. You only need to add the "APP_WEBVIEW" string to the userAgent on the iOS side
The above introduces how PHP determines whether a web page is loaded by Android webview, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.