serbanghita / Mobile-Detect

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
http://mobiledetect.net
MIT License
10.52k stars 2.67k forks source link

How to get required information instead of checking with is* methods? #923

Closed tanmayk closed 11 months ago

tanmayk commented 1 year ago

How to directly get browser, it's version, mobile or tablet, it's OS & OS version. I know there are methods to like isAndroid(), isChrome() etc, but we will need to check each & every method to find correct browser, os, right?.

Is there something that will just return e.g. name of the browser. something like $browser = $detect->getBrowser() or $browser_version = $detect->getBrowserVersion(); ?

pikulsky commented 1 year ago

@tanmayk , for now I use the following way:

    public function getBrowser()
    {
        // note: $this->mobileDetect below is \Detection\MobileDetect      
        $browsers = array_keys($this->mobileDetect->getBrowsers());
        foreach ($browsers as $browser) {
            if ($this->mobileDetect->is($browser)) {
                return $browser;
            }
        }
        // unknown browser
        return self::UNKNOWN_BROWSER;
    }

the only thing I noticed: the library detects mobile browsers, for desktop browser (at least for my UA "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36") the result for isChrome() = false. So, for browser name detection I have to use another library.

pikulsky commented 1 year ago

@tanmayk , same way call is() for values from $detect->getOperatingSystems() for get OS.

serbanghita commented 11 months ago

@tanmayk I realized that this can be confusing but the library was never intended to be a User-Agent detector but rather a detector for "mobile" and "tablet" devices. I tried to tackle this problem accurately a few years ago, but it was a cumbersome task.

https://github.com/serbanghita/Mobile-Detect/blob/d4279b39107b541a770a3b7e77abe4944785c538/src/MobileDetect.php#L674-L679