8

I am using following code for checking whether the user opened the site in App or not

 $ua = strtolower($_SERVER['HTTP_USER_AGENT']); if(stripos($ua,'android') && stripos($ua,'mobile') !== false) { if($_SERVER['HTTP_X_REQUESTED_WITH'] == "apppackagename") { echo "Opening with App"; } } 

But this is not working in some devices like.

GT - S7582 Android Version 4.2.2

Is there any solution for this to work in old version devices?

Thanks in advance !

10
  • What do you want to fix? Commented Jul 13, 2016 at 13:46
  • @greenapps please check my edit Commented Jul 13, 2016 at 13:48
  • What you want to fix is unclear. Commented Jul 13, 2016 at 13:49
  • The error which is displaying. or is there any other method to check whether site is opened from app or not in php ? Commented Jul 13, 2016 at 13:50
  • 1
    Use phpinfo() to see which indexes are implemented for the used php version on that server. Commented Jul 14, 2016 at 7:51

3 Answers 3

4
+50

You can simply include in the top https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php It's a lightweight library and requires to include only a single file.

for example:

 require_once('Mobile_Detect.php'); $device = new Mobile_Detect(); if ($device->isMobile() ) { if( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "apppackagename") { echo "Opening with App"; } } 

if you want to detect android then you can type:

$device->isAndroidOS() 

It's the most reliable way to detect a mobile device ( but also no bullet proof ). There is no way for reliable mobile detection with simple User Agent Check.

If you look into the source https://raw.githubusercontent.com/serbanghita/Mobile-Detect/master/Mobile_Detect.php, you can see that GT-S7582 is supported.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd recommend to use a library that has a stable set of checks for mobile/app detection. The upside of this is that you can expect the framework to support upcoming devices by simply upgrading your library instead of recoding it yourself.

For PHP there seems to be Mobile-Detect, it is open source and has active contributions: https://github.com/serbanghita/Mobile-Detect

1 Comment

Thanks for the reply :).This is helpful. But I don't want to use library
1

If you want hide the error you need use array_key_exists in you code like this:

$ua = strtolower($_SERVER['HTTP_USER_AGENT']); if(stripos($ua,'android') && stripos($ua,'mobile') !== false) { if(array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) { if($_SERVER['HTTP_X_REQUESTED_WITH'] == "apppackagename") { echo "Opening with App"; } } else { echo "Sorry... I don't see a package!"; } } 

The function array_key_exists "Checks if the given key or index exists in the array".

Maybe in the future you need hide another errors, so, you can use @ to mute error. See here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.