0

In my main.php file in the main root i'm using:

$isWebView = false;
    if((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false)) :
    $isWebView = true;
    elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH'])) :
    $isWebView = true;
    endif;

echo json_encode($isWebView);

to check if is webview or browser and i save result as a boolean in the $isWebView var.

now i'm trying to load that result in my main.js file inside scripts folder like below but it doesn't work, why? i get error data is not defined.

$.ajax({
    url: 'main.php',
    type: 'POST',
    dataType: 'json',
    data: data,
    success: function(data){
        console.log(data);
    }
  });
  • 1
    You have not defined the value `data: data` in the ajax reqeust code most likely. either relplace it with `{}` or with the value you want to post. and a sidenote, why are you posting without the need to. you should be adhering to rest standards and use get requests for this kind of thing – YouriKoeman Oct 23 '18 at 22:52
  • 1
    Also, why don't you use javascript to get useragent? Less work on your server that way. – Sneakybastardd Oct 23 '18 at 23:11
  • Is it possible to do it entirely in js? Even the HTTP_X_REQUESTED_WITH part? Cause for instance im using mobiledetect.js for some other user agent queries but it doesn't allow you to detect whether is a webview or web browser –  Oct 23 '18 at 23:17
  • No you can't detect webview for android through javascript, only ios. You can check this page for more info: https://stackoverflow.com/questions/37591279/detect-if-user-is-using-webview-for-android-ios-or-a-regular-browser – Sneakybastardd Oct 23 '18 at 23:38
  • Yea that's why i'm using php, toke the HTTP_X_REQUESTED_WITH suggestion from there –  Oct 23 '18 at 23:42

1 Answers1

0

Define the value of data inside the ajax request call

$.ajax({
    url: 'main.php',
    type: 'POST',
    dataType: 'json',
    data: {},
    success: function(result){
        try{
          JSON.parse(result);
          console.log(result.isWebView)
        } catch (err) {
          console.log("non json respone");
        }
    }
  });

Inside the php

$response = [];

$response['isWebView'] = false;

if ( (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false) ) {
    $response['isWebView'] = true;
} elseif( isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
    $response['isWebView'] = true;
}

echo json_encode($response);
YouriKoeman
  • 768
  • 3
  • 10
  • What you mean for value of data? tried to place my variable there as {isWebView} and {$isWebView} both it says it's not defined. –  Oct 24 '18 at 08:58
  • data is what gets send to the server inside the http post request. the parameter `data` inside the success function is an entirely different variable [inside a different lexical scope] – YouriKoeman Oct 24 '18 at 10:17
  • In my case if i want to retrieve the boolean value of $isWebView what should i write in data: {} ? –  Oct 24 '18 at 10:25
  • I'm trying to learn from your answer @YouriKoeman but without an example was hard to understand. I'm new to back-end stuff. –  Oct 24 '18 at 11:56
  • With the try catch, it logs err "non json respons", getting rid of it and doing console.log directly it logs "true" so seems it passes through, but looks like the suggested method to check if is a webview doesn't work at the end cause it should report "false" since i'm testing it from a laptop. –  Oct 24 '18 at 12:33
  • It was the: elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH'])) : , i used a different check with $_SERVER and now works, have to test it on the actual app. thank you! –  Oct 24 '18 at 12:50