0

Im working off of an app template that creates a webview for my site. I need a way to hide a div depending on whether of not that user is using my webview android app.

So far I have found the following php code:

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app") {
    echo 'WebView';
} else{
    echo 'Not WebView';
}

With this code what else do I need to add in order to hide a specific div (my header in this case)?

Jworth
  • 1
  • The REQUESTED-WITH header will never have the value of a domain name. Best practice is add some identifier into the User-Agent header that you can detect server side. – marekful Jan 30 '18 at 00:52
  • Put the header in the 'else' part and it will not show up when the 'if' condition is met. – KIKO Software Jan 30 '18 at 00:53
  • @KIKOSoftware I'm having trouble getting this to work. Specifically I'm trying to hide my mobile header in my webview app but not when the user views my page in android chrome browser. Any other notes that might help? – Jworth Jan 30 '18 at 02:47
  • Please post an answer how you solved this. I need it too – Elated Coder Apr 10 '18 at 10:02

1 Answers1

1

I would recomend using the open source library made available at mobiledetect.net. You can then use their 'magic methods' to determine if your user is on android.

if( $detect->isAndroidOS() ){ }else{ }

From there it's as simple as placing your <div> inside the else, or applying the style to hide your <div>(<style>.divsClass{display:none;}</style>), or by loading an additional JS or CSS file. There are a lot of variations on how... and depending on your specific scenario, the best option might change.

TCooper
  • 1,545
  • 1
  • 11
  • 19
  • Can this differentiate between androidOS and webview within android? – Jworth Jan 30 '18 at 02:26
  • I have to admit I glanced over the webview aspect and oversimplified your question. I've never needed to differentiate between android and android's webview. I'd check out this answer and those linked within it: https://stackoverflow.com/questions/37591279/detect-if-user-is-using-webview-for-android-ios-or-a-regular-browser – TCooper Jan 31 '18 at 17:00
  • Also, thanks for leading me to learn more about this topic - did more research than I expected when I first saw your question. – TCooper Jan 31 '18 at 17:01