How to get device type in php

I was wondering, until now, why someone had not posted a slightly alteration of the accepted answer to the use of implode() in order to have a better readability of the code. So here it goes:

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    We often find it important to detect the browser of our user to provide a better display experience. Few websites are mandatory to be accessed from a PC and not from mobile. Also, it acts as a precautionary measure for careless users to fill important forms from a smaller display like that of mobile.

    Using HTTP_USER_AGENT: We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP.

    Syntax:

    $_SERVER['HTTP_USER_AGENT']

    .

    Here the $_SERVER is a special reserved PHP variable that contains all web server information. It is known as a superglobal. These special variables were introduced in PHP 4.1.0. Next we need to read the message returned by the HTTP_USER_AGENT to pass the control to the next set of instructions. For demonstration purpose we will put an echo”” statement to confirm a mobile device is detected. We will read the HTTP_USER_AGENT’s returned message with preg_match() function. It performs a regular expression match.

    Example: It’s easy to get lost in this huge chunk of regex but it’s like this to detect every sort of browser from every mobile Operating system available in the market(can also detect Kindle devices). For example, (android|bb\d+|meego).+mobile|avantgo|bada will check if the user device’s operating system is Android or not. If this snippet is inserted into the index.php of the website and the site is accessed from a mobile device, the browser will show the message as Mobile Browser Detected.

    function isMobileDevice() {

        return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo

    |fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i"

    , $_SERVER["HTTP_USER_AGENT"]);

    }

    if(isMobileDevice()){

        echo "Mobile Browser Detected";

    }

    else {

        echo "Mobile Browser Not Detected";

    }

    ?>

    Output: We are accessing it from a laptop.

    Mobile Browser Not Detected

    How can I get device detail in PHP?

    php $useragent=$_SERVER['HTTP_USER_AGENT']; if(preg_match('/(android|bb\d+|meego)..
    Unzip the file into a directory in your PHP server..
    Then add the following code to your PHP page:.
    All available device information will be contained in $_51d array:.

    How can you detect a mobile device using PHP?

    Here is the code: php $useragent=$_SERVER['HTTP_USER_AGENT']; if(preg_match('/(android|bb\d+|meego). +mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|

    How check PHP or mobile in PC?

    An easy way to detect a mobile or desktop device in PHP is to check if the HTTP user agent contains the word “mobile”. $ua = strtolower($_SERVER["HTTP_USER_AGENT"]); $isMob = is_numeric(strpos($ua, "mobile"));

    How does my tablet detect PHP?

    The only way to do this is with a huge lookup table of User-Agent: strings. get_browser() would probably be able to do what you want, but you would need to make sure that you keep the browscap file very up to date - new tablet models are being released on a weekly basis.