Android WebView detection in PHP

I've done a simple Android App with native menu and a webview with my site content. I need to detect if my website is included in a webview in order to hide the menu bar.

After a long research I found this way:

if($_SERVER['HTTP_X_REQUESTED_WITH'] == "myAppPackage"){
    //the site is included in webview
}

This solution is good for a lot of devices, but for a Galaxy S4 Mini(Android 4.2.2) this variable is blank!

Other htp header variables:

  • PATH /usr/local/bin:/usr/bin:/bin
  • UNIQUE_ID U6LV8AAAEAABFtKXcAAADY
  • HTTP_HOST hostname
  • HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
  • HTTP_USER_AGENT Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
  • HTTP_REFERER http://webx225.aruba.it/CP/index.php
  • HTTP_ACCEPT_ENCODING gzip,deflate,sdch
  • HTTP_ACCEPT_LANGUAGE it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
  • HTTP_COOKIE PHPSESSID=5deb3527097d0f767aba45d0aa042; acopendivids=nada; acgroupswithpersist=nada; wooTracker=lzp5HpPQNDCd; __utma=250126209.968543423.1402584011.1403102882.1403106109.27; __utmc=250126209; __utmz=250126209.1402752662.8.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)
  • HTTP_CACHE_CONTROL max-stale=0
  • HTTP_CONNECTION Keep-Alive
  • HTTP_X_BLUECOAT_VIA b1ae316f3a2874e7
  • SERVER_SIGNATURE no value
  • SERVER_SOFTWARE Apache/2.2
  • SERVER_NAME hostname
  • SERVER_ADDR 62.149.140.235
  • SERVER_PORT 80
  • REMOTE_ADDR 62.249.32.77
  • DOCUMENT_ROOT /web/htdocs/hostname/home/
  • SERVER_ADMIN postmaster@hostname
  • SCRIPT_FILENAME /web/htdocs/hostname/home/ aruba__php__test .php
  • REMOTE_PORT 43924
  • GATEWAY_INTERFACE CGI/1.1
  • SERVER_PROTOCOL HTTP/1.1
  • REQUEST_METHOD GET
  • QUERY_STRING no value
  • REQUEST_URI / aruba__php__test .php
  • SCRIPT_NAME / aruba__php__test .php
  • PHPRC no value

  • Thanks to greenapps idea, this is the final solution.

    Android APP MainActivity :

    public View onCreateView
    ....
    
    WebView webView = (WebView) rootView.findViewById(R.id.my_webview);
    
    
    String agentModified = webView.getSettings().getUserAgentString().concat(" MobileApplication(mypackage)");
    webView.getSettings().setUserAgentString(agentModified);
    

    Web site :

    if(strpos($_SERVER['HTTP_USER_AGENT'], 'com.vivicrema.android') !== false)
        $isMobileApp = true;
    

    It works like a charm!

    链接地址: http://www.djcxy.com/p/48804.html

    上一篇: HttpClient POST生成GET

    下一篇: PHP中的Android WebView检测