Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/01/2013, 05:10
underwebinfo
 
Fecha de Ingreso: septiembre-2012
Ubicación: Buenos aires
Mensajes: 110
Antigüedad: 11 años, 7 meses
Puntos: 9
Sonrisa Detectar Navegador, Versión y Sistema Operativo

Hola , esta noche me encontre con la necesidad de detectar el navegador y su version con php, y me puse en la tarea de buscarla por internet, no habia una que me convenciera asi que saque trozos de codigos y los fui adaptando lo probe en todos los navegadores y en todas sus versiones, funciona correctamente, no tengo ni idea si en mac o masitosh funciona no tengo esos sistemas xD, le agregaria android y todo lo demás ... pero no tengo smartphone como para probarlo :p

Código PHP:
Ver original
  1. # Browser
  2.     class Browser{
  3.  
  4.         # Forma de uso
  5.         # $Nav  = new Browser;
  6.         # $Nav->Iniciar();  # Inicia el constructor
  7.         # $Nav->Navegador;  # Devuelve el navegador [String]
  8.         # $Nav->Version;    # Devuelve la version [Int:Entero]
  9.         # $Nav->Sistemao;   # Devuelve el Sistema Operativo [String]
  10.        
  11.         # Variables
  12.         public $User_Agent  = NULL;
  13.         public $Navegador   = NULL;
  14.         public $Version     = NULL;
  15.         public $SistemaO    = NULL;
  16.        
  17.         # Constructor
  18.         public function Iniciar(){
  19.            
  20.             # Constructor
  21.             $this->User_Agent = $_SERVER['HTTP_USER_AGENT'];
  22.            
  23.             # Funciones
  24.             $this->Navegador();
  25.             $this->Version();
  26.             $this->SO();
  27.         }
  28.    
  29.         # Detectar
  30.         private function Navegador(){
  31.            
  32.             if(preg_match('/MSIE/i',$this->User_Agent))         $this->Navegador = "MSIE";
  33.             if(preg_match('/Opera/i',$this->User_Agent))        $this->Navegador = 'Opera';
  34.             if(preg_match('/Firefox/i',$this->User_Agent))      $this->Navegador = 'Firefox';
  35.             if(preg_match('/Safari/i',$this->User_Agent))       $this->Navegador = 'Safari';
  36.             if(preg_match('/Chrome/i',$this->User_Agent))       $this->Navegador = 'Chrome';
  37.            
  38.         }
  39.    
  40.         # Version
  41.         private function Version(){
  42.            
  43.            
  44.             if($this->Navegador!=='Opera' && preg_match("#(".strtolower($this->Navegador).")[/ ]?([0-9.]*)#", strtolower($this->User_Agent), $match))
  45.                 $this->Version = floor($match[2]);
  46.            
  47.             if($this->Navegador=='Opera' || $this->Navegador=='Safari' && preg_match("#(version)[/ ]?([0-9.]*)#", strtolower($this->User_Agent), $match))
  48.                 $this->Version = floor($match[2]);
  49.                
  50.         }
  51.        
  52.        
  53.         # Sistema Operativo
  54.         private function SO(){
  55.            
  56.             if(preg_match("/win/i", $this->User_Agent))             $this->SistemaO = 'Windows';
  57.             if(preg_match("/linux/i", $this->User_Agent))   $this->SistemaO = 'Linux';
  58.             if(preg_match("/mac/i", $this->User_Agent))     $this->SistemaO = 'Macintosh';
  59.            
  60.         }
  61.  
  62.     }