YouTube Icon

How to Get User Country Name, Browser, OS Name, Device Name in PHP




How to Get User Country Name, Browser, OS Name, Device Name in PHP

Using PHP get stoner details IP address country, device, cybersurfer, operating system 
 
 Are you interested in rooting precious information about your website callers, similar as their country, cybersurfer, and operating system? With PHP, you can fluently gain this data and enhance your website's functionality. By exercising colorful PHP functions and APIs, you can recoup the stoner's country grounded on their IP address, prize their cybersurfer information, and identify the operating system they're using. To get the stoner's country, you can work IP geolocation services or APIs, similar as MaxMind or GeoIP. These services give you with the stoner's geographical position grounded on their IP address, allowing you to epitomize content or acclimate settings consequently. To determine the stoner's cybersurfer information, you can use the$, Garçon('HTTP_USER_AGENT') variable and parse it using PHP's erected- in functions. This information can help you optimize your website's design and functionality for different cybersurfers. Incipiently, to recoup the operating system name, you can use the$, Garçon('HTTP_USER_AGENT') variable and identify specific patterns or keywords that correspond to colorful operating systems. By enforcing these PHP ways, you can gain precious perceptivity about your website callers and deliver a more customized stoner experience. 

user-information.php

<?php

class UserInformation {
    private $agent = "";
    private $browsers = array(
        'SamsungBrowser'=> 'Samsung Browser',
        'MiuiBrowser'   => 'Xiaomi Browser',
        'NokiaBrowser'  => 'Nokia Browser',
        'UCBrowser'     => 'UC Browser',
        'OPR'           => 'Opera',
        'Flock'         => 'Flock',
        'Edge'          => 'Edge',
        'edg'           => 'Edge',
        'Maxthon'       => 'Maxthon',           
        'Opera.*?Version' => 'Opera',
        'Opera'         => 'Opera',
        'MSIE'          => 'Internet Explorer',
        'Internet Explorer' => 'Internet Explorer',
        'Trident.* rv'  => 'Internet Explorer',
        'YaBrowser'     => 'Yandex Browser',
        'Chrome'        => 'Chrome',
        'Shiira'        => 'Shiira',
        'Firefox'       => 'Firefox',
        'Chimera'       => 'Chimera',
        'Phoenix'       => 'Phoenix',
        'Firebird'      => 'Firebird',
        'Camino'        => 'Camino',
        'Netscape'      => 'Netscape',
        'OmniWeb'       => 'OmniWeb',
        'Safari'        => 'Safari',
        'Mozilla'       => 'Mozilla',
        'Konqueror'     => 'Konqueror',
        'icab'          => 'iCab',
        'Lynx'          => 'Lynx',
        'Links'         => 'Links',
        'hotjava'       => 'HotJava',
        'amaya'         => 'Amaya',
        'IBrowse'       => 'IBrowse',
        'Ubuntu'        => 'Ubuntu Web Browser',
        
    );
    private $device_name = array(
        'MI '           => 'MI',
        'vivo'          => 'Vivo',
        'Ubuntu'        => 'Ubuntu',
        'S25'           => 'Smart S-25',
        'Moto G'        => 'Moto G',
        'LAVA'          => 'LAVA',
        'RMX'           => 'Oppo Realme',
        'Lenovo'        => 'Lenovo',
        'iPhone'        => 'iPhone',
        'pixel'         => 'Google Pixel',
        'BB'            => 'BlackBerry',
        'HUAWEI'        => 'HUAWEI',
        'LM'            => 'LG',
        'LGM'           => 'LG',
        'LG'            => 'LG',
        'windows phone' => 'Windows Phone',
        'ASUS_'         => 'Asus ZenFone',
        'KFAPWI'        => 'Kindle Fire',
        'SM-'           => 'Samsung',
        'HONOR'         => 'Huawei Honor',
        'A37F'          => 'Oppo',
        'ONEPLUS'       => 'One Plus',
        'Karbonn'       => 'Karbonn',
        'XT'            => 'Motorola',
        'os x'          => 'Apple Mac',
        'mobileexplorer' => 'Mobile Explorer',
        'palmsource'    => 'Palm',
        'palmscape'     => 'Palmscape',
        'motorola'      => 'Motorola',
        'nokia'         => 'Nokia',
        'nexus'         => 'Nexus',
        'palm'          => 'Palm',
        
        'ipad'          => 'iPad',
        'ipod'          => 'Apple iPod Touch',
        'sony'          => 'Sony Ericsson',
        'ericsson'      => 'Sony Ericsson',
        'blackberry'    => 'BlackBerry',
        'cocoon'        => 'O2 Cocoon',
        'blazer'        => 'Treo',
        'lg'            => 'LG',
        'amoi'          => 'Amoi',
        'xda'           => 'XDA',
        'mda'           => 'MDA',
        'vario'         => 'Vario',
        'htc'           => 'HTC',
        'samsung'       => 'Samsung',
        'sharp'         => 'Sharp',
        'sie-'          => 'Siemens',
        'alcatel'       => 'Alcatel',
        'benq'          => 'BenQ',
        'ipaq'          => 'HP iPaq',
        'mot-'          => 'Motorola',
        'playstation portable'  => 'PlayStation Portable',
        'playstation 3'     => 'PlayStation 3',
        'playstation vita'      => 'PlayStation Vita',
        'hiptop'        => 'Danger Hiptop',
        'nec-'          => 'NEC',
        'panasonic'     => 'Panasonic',
        'philips'       => 'Philips',
        'sagem'         => 'Sagem',
        'sanyo'         => 'Sanyo',
        'spv'           => 'SPV',
        'zte'           => 'ZTE',
        'sendo'         => 'Sendo',
        'nintendo dsi'  => 'Nintendo DSi',
        'nintendo ds'   => 'Nintendo DS',
        'nintendo 3ds'  => 'Nintendo 3DS',
        'wii'           => 'Nintendo Wii',
        'open web'      => 'Open Web',
        'openweb'       => 'OpenWeb',
        'android'       => 'Android',
        'symbian'       => 'Symbian',
        'SymbianOS'     => 'SymbianOS',
        'elaine'        => 'Palm',
        'series60'      => 'Symbian S60',
        'windows ce'    => 'Windows CE',
        'windows'       => 'Windows',
        'obigo'         => 'Obigo',
        'netfront'      => 'Netfront Browser',
        'openwave'      => 'Openwave Browser',
        'mobilexplorer' => 'Mobile Explorer',
        'operamini'     => 'Opera Mini',
        'opera mini'    => 'Opera Mini',
        'opera mobi'    => 'Opera Mobile',
        'fennec'        => 'Firefox Mobile',
        'digital paths' => 'Digital Paths',
        'avantgo'       => 'AvantGo',
        'xiino'         => 'Xiino',
        'novarra'       => 'Novarra Transcoder',
        'vodafone'      => 'Vodafone',
        'docomo'        => 'NTT DoCoMo',
        'o2'            => 'O2',
        'mobile'        => 'Generic Mobile',
        'wireless'      => 'Generic Mobile',
        'j2me'          => 'Generic Mobile',
        'midp'          => 'Generic Mobile',
        'cldc'          => 'Generic Mobile',
        'up.link'       => 'Generic Mobile',
        'up.browser'    => 'Generic Mobile',
        'smartphone'    => 'Generic Mobile',
        'cellphone'     => 'Generic Mobile',
        'Linux'         => 'Linux'
    );
    private $os = array(
        'windows nt 10.0'   => 'Windows 10',
        'windows nt 6.3'    => 'Windows 8.1',
        'windows nt 6.2'    => 'Windows 8',
        'windows nt 6.1'    => 'Windows 7',
        'windows nt 6.0'    => 'Windows Vista',
        'windows nt 5.2'    => 'Windows 2003',
        'windows nt 5.1'    => 'Windows XP',
        'windows nt 5.0'    => 'Windows 2000',
        'windows nt 4.0'    => 'Windows NT 4.0',
        'winnt4.0'          => 'Windows NT 4.0',
        'winnt 4.0'         => 'Windows NT',
        'winnt'             => 'Windows NT',
        'windows 98'        => 'Windows 98',
        'win98'             => 'Windows 98',
        'windows 95'        => 'Windows 95',
        'win95'             => 'Windows 95',
        'windows phone'     => 'Windows Phone',
        'windows'           => 'Unknown Windows OS',
        'android'           => 'Android',
        'blackberry'        => 'BlackBerry',
        'BB'                => 'BlackBerry',
        'iphone'            => 'iOS',
        'ipad'              => 'iOS',
        'ipod'              => 'iOS',
        'os x'              => 'Mac OS X',
        'ppc mac'           => 'Power PC Mac',
        'freebsd'           => 'FreeBSD',
        'ppc'               => 'Macintosh',
        'Linux'             => 'Linux',
        'debian'            => 'Debian',
        'sunos'             => 'Sun Solaris',
        'beos'              => 'BeOS',
        'apachebench'       => 'ApacheBench',
        'aix'               => 'AIX',
        'irix'              => 'Irix',
        'osf'               => 'DEC OSF',
        'hp-ux'             => 'HP-UX',
        'netbsd'            => 'NetBSD',
        'bsdi'              => 'BSDi',
        'openbsd'           => 'OpenBSD',
        'gnu'               => 'GNU/Linux',
        'unix'              => 'Unknown Unix OS',
        'symbian'           => 'Symbian OS',
        'Nokia'             => 'Nokia Mobile',
    );

    function __construct() {
        $this->agent = $_SERVER['HTTP_USER_AGENT'];
    }

    public function getBrowser() {
        $browser_name = "Unknown Browser";
        foreach ($this->browsers as $key => $val) {
            if (preg_match('|'.$key.'.|i', $this->agent, $match)) {
                $browser_name = $val;
                break;
            }
        }
        return $browser_name;
    }

    public function getOs() {
        $device_name = "Unknown Platform";
        foreach ($this->os as $key => $val) {
            if (preg_match('|'.preg_quote($key).'|i', $this->agent)) {
                $device_name = $val;
                break;
            }
        }
        return $device_name;
    }

    public function getDeviceName() {
        $mobile = "unknown";
        foreach ($this->device_name as $key => $val) {
            if (FALSE !== (stripos($this->agent, $key))) {
                $mobile = $val;
                break;
            }
        }
        return $mobile;
    }

    public  function getCountry() {
        $output = array(
            "city"           => 'unknown',
            "state"          => '',
            "country"        => '',
            "country_code"   => '',
            "continent"      => '',
            "continent_code" => ''
        );
        $ip = $this->getIpAddress();
        $continents = array(
            "AF" => "Africa",
            "AN" => "Antarctica",
            "AS" => "Asia",
            "EU" => "Europe",
            "OC" => "Australia (Oceania)",
            "NA" => "North America",
            "SA" => "South America"
        );

        if (filter_var($ip, FILTER_VALIDATE_IP)) {

            $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));

            if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {

                $output = array(
                    "city"           => @$ipdat->geoplugin_city,
                    "state"          => @$ipdat->geoplugin_regionName,
                    "country"        => @$ipdat->geoplugin_countryName,
                    "country_code"   => @$ipdat->geoplugin_countryCode,
                    "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                    "continent_code" => @$ipdat->geoplugin_continentCode
                );
            }
        }
        return $output;
    }

    public function getDevice() {
        $tablet_browser = 0;
        $mobile_browser = 0;
        if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($this->agent))) {
            $tablet_browser++;
        }
        if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($this->agent))) {
            $mobile_browser++;
        }
        if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
            $mobile_browser++;
        }
        $mobile_ua = strtolower(substr($this->agent, 0, 4));
        $mobile_agents = array(
            'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
            'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
            'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
            'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
            'newt','noki','palm','pana','pant','phil','play','port','prox',
            'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
            'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
            'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
            'wapr','webc','winw','winw','xda ','xda-');
        if (in_array($mobile_ua,$mobile_agents)) {
            $mobile_browser++;
        }
        if (strpos(strtolower($this->agent),'opera mini') > 0) {
            $mobile_browser++;
            $stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
            if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
                $tablet_browser++;
            }
        }
        if ($tablet_browser > 0) {
            return 'Tablet';
        }
        else if ($mobile_browser > 0) {
            return 'Mobile';
        }
        else {
            return 'Desktop';
        }   
    }

    public function getIpAddress() {
        $mainIp = '';
        if(getenv('REMOTE_ADDR'))
            $mainIp = getenv('REMOTE_ADDR');
        else
            $mainIp = 'UNKNOWN';
        return $mainIp;
    }
}
?>

user.php

<?php 
    require './user-information.php';
    $user_data = new UserInformation();
    echo $user_data->getIpAddress();
    echo $user_data->getBrowser();
    echo $user_data->getOs();
    echo $user_data->getDevice();
    echo $user_data->getDeviceName();
    $location = $user_data->getCountry();
    echo $location['country'];
    echo $location['state'];
    echo $location['city'];
    echo $location['country_code'];
    echo $location['continent'];
    echo $location['continent_code'];
 ?>

index.php

<!DOCTYPE html>
<html>
<head>
    <title>User Information</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
#myTable {
    font-family: 'helvetica neue', helvetica, arial, 'lucida grande', sans-serif;
    border-collapse: collapse;
}
#myTable td {
    border: solid 1px #e4e4e4;
    padding: 10px;
    font-size: 15px;
    text-align: left;
    font-weight: bold;
    color: #7d7d7d;
}
#myTable td>div {
    font-weight: normal;
}
#myTable tr {
    background-color: #fbfbfb;
    color: #000000;
}
#myTable tr:hover {
    background-color: #FFFFFF;
    color: #000000;
}
</style>

<body>
    <?php
    require './user-information.php';
    $userInfo = new UserInformation();
    $location = $userInfo->getCountry();

    echo '<table id="myTable" width="100%">
    <tbody>
    <tr>
    <td>Country</td>
    <td><div>'.$location['country'].'</div></td>
    </tr>

    <tr>
    <td>State</td>
    <td><div>'.$location['state'].'</div></td>
    </tr>

    <tr>
    <td>City</td>
    <td><div>'.$location['city'].'</div></td>
    </tr>

    <tr>
    <td>country code</td>
    <td><div>'.$location['country_code'].'</div></td>
    </tr>

    <tr>
    <td>Continent</td>
    <td><div>'.$location['continent'].'</div></td>
    </tr>

    <tr>
    <td>Continent code</td>
    <td><div>'.$location['continent_code'].'</div></td>
    </tr>

    <tr>
    <td>IP Address</td>
    <td><div>'.$userInfo->getIpAddress().'</div></td>
    </tr>

    <tr>
    <td>Browser</td>
    <td><div>'.$userInfo->getBrowser().'</div></td>
    </tr>

    <tr>
    <td>Operating system</td>
    <td><div>'.$userInfo->getOs().'</div></td>
    </tr>

    <tr>
    <td>Device</td>
    <td><div>'.$userInfo->getDevice().'</div></td>
    </tr>

    <tr>
    <td>Device name</td>
    <td><div>'.$userInfo->getDeviceName().'</div></td>
    </tr>

    </tbody>
    </table>';
?>
</body>
</html>

 



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink

Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala
CFT

News & Blogs

4e001a829dec0246019fc8cfbef37760.png

How to detect network connection is Online/Offl...

We all might be familiar with the JavaScript way of checking Online/ Offline status. But in the c...

ff7f9eca4d1a0d29656644f11b806865.png

How to Drag And Drop Data Between Two Tables In...

Introduction  occasionally you need to drag and drop data from one table to another table...

fd0e5c6e4196603ef85979b1bc50caf4.jpeg

Top 10 Amazing Books For Every Designers

There are innumerous books out there that can help you come a better developer, whether you are l...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh
CFT

Our Client Says

WhatsApp Chat with Our Support Team