Hola,

explorer me pidió que le ayudara a recopilar información de los visitantes de su web. Hice algo rápido y aquí lo dejo:
$log = fopen('log.txt', 'a');
	
	fwrite($log, "[ ------- ".date('d/m/Y H:i:s a', time())." ------- ]".PHP_EOL);
	fwrite($log, "\t[ IP ] = ".$_SERVER['REMOTE_ADDR'].PHP_EOL);
	fwrite($log, "\t[ PAGE ] = ".$_SERVER['PHP_SELF'].PHP_EOL);
	
	$server_vars = array('HTTP_REFERER', 'REMOTE_HOST', 'HTTP_ACCEPT_LANGUAGE', 'REMOTE_USER');
	foreach($server_vars as $server_var){
		if(isset($_SERVER[$server_var])) fwrite($log, "\t[ ".$server_var." ] = ".$_SERVER[$server_var].PHP_EOL);
	}

	$user_agent = parse_user_agent();
	fwrite($log, "\t[ ------- USER AGENT ------- ]".PHP_EOL);
	foreach($user_agent as $user_agent_var => $user_agent_var_value){
		fwrite($log, "\t\t[ ".$user_agent_var." ] = ".$user_agent_var_value.PHP_EOL);
	}
	fwrite($log, PHP_EOL);
	fclose($log);
	
	/**
	 * Parses a user agent string into its important parts
	 *
	 * @author Jesse G. Donat <[email protected]>
	 * @link https://github.com/donatj/PhpUserAgent
	 * @link http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT
	 * @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
	 * @throws InvalidArgumentException on not having a proper user agent to parse.
	 * @return string[] an array with browser, version and platform keys
	 */
	function parse_user_agent( $u_agent = null ) {
		if( is_null($u_agent) ) {
			if( isset($_SERVER['HTTP_USER_AGENT']) )  $u_agent = $_SERVER['HTTP_USER_AGENT'];
			else throw new \InvalidArgumentException('parse_user_agent requires a user agent');
		}

		$platform = null;
		$browser  = null;
		$version  = null;

		$empty = array( 'platform' => $platform, 'browser' => $browser, 'version' => $version );
		if( !$u_agent ) return $empty;
		if( preg_match('/\((.*?)\)/im', $u_agent, $parent_matches) ) {
			preg_match_all('/(?P<platform>BB\d+;|Android|CrOS|iPhone|iPad|Linux|Macintosh|Windows(\ Phone)?|Silk|linux-gnu|BlackBerry|PlayBook|(New\ )?Nintendo\ (WiiU?|3DS)|Xbox(\ One)?)
					(?:\ [^;]*)?
					(?:;|$)/imx', $parent_matches[1], $result, PREG_PATTERN_ORDER);

			$priority           = array( 'Android', 'Xbox One', 'Xbox' );
			$result['platform'] = array_unique($result['platform']);
			if( count($result['platform']) > 1 ) {
				if( $keys = array_intersect($priority, $result['platform']) )  $platform = reset($keys);
				else  $platform = $result['platform'][0];
			} elseif( isset($result['platform'][0]) ) {
				$platform = $result['platform'][0];
			}

			//Detección de versión de Windows by Blau (http://indetectables.net)
			if($platform == 'Windows'){
				$version = explode($platform, $u_agent);
				$version = $version[1];
				$bitness = (strpos(strtolower($version), 'wow64') !== false ? 'x64' : 'x86');
				$version = explode(';', $version);
				$version = $version[0];
				
				$is_nt = (strpos($version, 'NT') !== false);
				if($is_nt) $version = str_replace('NT', '', $version);
				
				$version = trim($version);
				$os_name = array(
					'5.1' => 'XP',
					'5.2' => ($is_nt ? 'XP 64-Bits' : 'Server 2003'),
					'6.0' => ($is_nt ? 'Vista' : 'Server 2008'),
					'6.1' => ($is_nt ? '7' : 'Server 2008 R2'),
					'6.2' => ($is_nt ? '8' : 'Server 2012'),
					'6.3' => ($is_nt ? '8.1' : 'Server 2012 R2'),
				);
				
				$platform .= ' '.$os_name[$version].' ['.$bitness.']';
				echo 'O.S: '.$platform;
			}
		}

		if( $platform == 'linux-gnu' ) $platform = 'Linux';
		elseif( $platform == 'CrOS' ) $platform = 'Chrome OS';

		preg_match_all('%(?P<browser>Camino|Kindle(\ Fire\ Build)?|Firefox|Iceweasel|Safari|MSIE|Trident|AppleWebKit|Chrome|
				IEMobile|Opera|OPR|Silk|Midori|Edge|
				Baiduspider|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl|
				NintendoBrowser|PLAYSTATION\ (\d|Vita)+)
				(?:\)?;?)
				(?:(?:[:/ ])(?P<version>[0-9A-Z.]+)|/(?:[A-Z]*))%ix',
				$u_agent, $result, PREG_PATTERN_ORDER);

		// If nothing matched, return null (to avoid undefined index errors)
		if( !isset($result['browser'][0]) || !isset($result['version'][0]) ) {
			if( !$platform && preg_match('%^(?!Mozilla)(?P<browser>[A-Z0-9\-]+)(/(?P<version>[0-9A-Z.]+))?([;| ]\ ?.*)?$%ix', $u_agent, $result)) return array( 'platform' => null, 'browser' => $result['browser'], 'version' => isset($result['version']) ? $result['version'] ?: null : null );
			return $empty;
		}

		if( preg_match('/rv:(?P<version>[0-9A-Z.]+)/si', $u_agent, $rv_result) )  $rv_result = $rv_result['version'];

		$browser = $result['browser'][0];
		$version = $result['version'][0];

		$find = function ( $search, &$key ) use ( $result ) {
			$xkey = array_search(strtolower($search), array_map('strtolower', $result['browser']));
			if( $xkey !== false ) {
				$key = $xkey;

				return true;
			}

			return false;
		};

		$key  = 0;
		$ekey = 0;
		if( $browser == 'Iceweasel' ) {
			$browser = 'Firefox';
		} elseif( $find('Playstation Vita', $key) ) {
			$platform = 'PlayStation Vita';
			$browser  = 'Browser';
		} elseif( $find('Kindle Fire Build', $key) || $find('Silk', $key) ) {
			$browser  = $result['browser'][$key] == 'Silk' ? 'Silk' : 'Kindle';
			$platform = 'Kindle Fire';
			if( !($version = $result['version'][$key]) || !is_numeric($version[0]) ) {
				$version = $result['version'][array_search('Version', $result['browser'])];
			}
		} elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) {
			$browser = 'NintendoBrowser';
			$version = $result['version'][$key];
		} elseif( $find('Kindle', $key) ) {
			$browser  = $result['browser'][$key];
			$platform = 'Kindle';
			$version  = $result['version'][$key];
		} elseif( $find('OPR', $key) ) {
			$browser = 'Opera Next';
			$version = $result['version'][$key];
		} elseif( $find('Opera', $key) ) {
			$browser = 'Opera';
			$find('Version', $key);
			$version = $result['version'][$key];
		} elseif( $find('Midori', $key) ) {
			$browser = 'Midori';
			$version = $result['version'][$key];
		} elseif( $browser == 'MSIE' || ($rv_result && $find('Trident', $key)) || $find('Edge', $ekey) ) {
			$browser = 'MSIE';
			if( $find('IEMobile', $key) ) {
				$browser = 'IEMobile';
				$version = $result['version'][$key];
			} elseif( $ekey ) {
				$version = $result['version'][$ekey];
			} else {
				$version = $rv_result ?: $result['version'][$key];
			}
		} elseif( $find('Chrome', $key) ) {
			$browser = 'Chrome';
			$version = $result['version'][$key];
		} elseif( $browser == 'AppleWebKit' ) {
			if( ($platform == 'Android' && !($key = 0)) ) {
				$browser = 'Android Browser';
			} elseif( strpos($platform, 'BB') === 0 ) {
				$browser  = 'BlackBerry Browser';
				$platform = 'BlackBerry';
			} elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) {
				$browser = 'BlackBerry Browser';
			} elseif( $find('Safari', $key) ) {
				$browser = 'Safari';
			}

			$find('Version', $key);

			$version = $result['version'][$key];
		} elseif( $key = preg_grep('/playstation \d/i', array_map('strtolower', $result['browser'])) ) {
			$key = reset($key);

			$platform = 'PlayStation ' . preg_replace('/[^\d]/i', '', $key);
			$browser  = 'NetFront';
		}

		return array( 'platform' => $platform ?: null, 'browser' => $browser ?: null, 'version' => $version ?: null );

	}
Nota:
La función [Enlace externo eliminado para invitados] la he editado para que detecte la versión de Windows y los bits (x86 ó x64).

EDIT:
Código modificado para que funcione en versiones de PHP menores a 5.4.
Responder

Volver a “PHP”