<?php

$user_agent = $_SERVER['HTTP_USER_AGENT'];

function getOS() { 
    global $user_agent;
    $os_platform    =   "Unknown OS Platform";
    $os_array       =   array(
        '/windows nt 10/i'      =>  'Windows',
        '/windows nt 6.3/i'     =>  'Windows',
        '/windows nt 6.2/i'     =>  'Windows',
        '/windows nt 6.1/i'     =>  'Windows',
        '/windows nt 6.0/i'     =>  'Windows',
        '/windows nt 5.2/i'     =>  'Windows',
        '/windows nt 5.1/i'     =>  'Windows',
        '/windows xp/i'         =>  'Windows',
        '/windows nt 5.0/i'     =>  'Windows',
        '/windows me/i'         =>  'Windows',
        '/win98/i'              =>  'Windows',
        '/win95/i'              =>  'Windows',
        '/win16/i'              =>  'Windows',
        '/macintosh|mac os x/i' =>  'Mac',
        '/mac_powerpc/i'        =>  'Mac',
        '/linux/i'              =>  'Linux',
        '/ubuntu/i'             =>  'Ubuntu',
        '/iphone/i'             =>  'iPhone',
        '/ipod/i'               =>  'iPod',
        '/ipad/i'               =>  'iPad',
        '/android/i'            =>  'Android',
        '/blackberry/i'         =>  'BlackBerry',
        '/webos/i'              =>  'Mobile'
    );

    foreach ($os_array as $regex => $value) { 
        if (preg_match($regex, $user_agent)) {
            $os_platform = $value;
        }
    }
    return $os_platform;
}

function getBrowser()
{

  global $user_agent;

  $browser        =   "Unknown Browser";

  $browser_array  =   array(
    '/msie/i'       =>  'Internet Explorer',
    '/firefox/i'    =>  'Firefox',
    '/safari/i'     =>  'Safari',
    '/chrome/i'     =>  'Chrome',
    '/opera/i'      =>  'Opera',
    '/netscape/i'   =>  'Netscape',
    '/maxthon/i'    =>  'Maxthon',
    '/konqueror/i'  =>  'Konqueror',
    '/mobile/i'     =>  'Handheld Browser'
  );

  foreach ($browser_array as $regex => $value) {

    if (preg_match($regex, $user_agent)) {
      $browser    =   $value;
    }
    if (preg_match('|MSIE ([0-9].[0-9]{1,2})|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Internet Explorer';
    } elseif (preg_match('|Opera/([0-9].[0-9]{1,2})|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Opera';
    } elseif (preg_match('|Firefox/([0-9\.]+)|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Mozila Firefox';
    } elseif (preg_match('|Chrome/([0-9\.]+)|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Google Chrome';
    } elseif (preg_match('|Safari/([0-9\.]+)|', $user_agent, $matched)) {
      $browser_version = $matched[1];
      $browser = 'Safari';
    } else {
      // browser not recognized!
      $browser_version = 0;
      //$browser= 'other';
    }
  }

  return $browser;
}

function my_file_get_contents( $site_url ) {
  $ch      = curl_init();
  $timeout = 5;
  curl_setopt ($ch, CURLOPT_URL, $site_url);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  ob_start();
  curl_exec($ch);
  curl_close($ch);
  $file_contents = ob_get_contents();
  ob_end_clean();
  return $file_contents;
}

$ip           = isset($_SERVER['HTTP_CLIENT_IP'])
? $_SERVER['HTTP_CLIENT_IP']
: (isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    ? $_SERVER['HTTP_X_FORWARDED_FOR']
    : $_SERVER['REMOTE_ADDR']);
// $url          = my_file_get_contents("http://ip-api.com/json/" . $ip);
// $ip = "45.189.75.214";
$url          = file_get_contents("http://ip-api.com/json/" . $ip);
$dados        = json_decode($url, true);
$pais         =  $dados['country']; 
$estado = $dados['regionName'];
$cidade = $dados['city'];
$cidade_state = $cidade." ".$estado." - ".$pais;
$user_os      =   getOS();
$user_browser =   getBrowser();
$browser =   $user_browser;

?>