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.

Home Page:http://mobiledetect.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

tanmayk opened this issue · comments

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(); ?

@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.

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

@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.

/**
* List of mobile User Agents.
*
* IMPORTANT: This is a list of only mobile browsers.
* Mobile Detect 2.x supports only mobile browsers,
* it was never designed to detect all browsers.