Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it L'avatar di the-bit
    Registrato dal
    Feb 2005
    Messaggi
    543

    Come estrarre sistema operativo e browser usati a partire da User Agent?

    Salve a tutti,
    vorrei sapere come fare ad estrarre, a partire dalla lunga lista di codici e parole del risultato che mi resituisce User Agent, il sistema operativo e il browser in chiaro.
    Grazie.
    "To iterate is human, to recurse, divine." (R.(Heller))

  2. #2
    Browser:
    Codice PHP:
    $useragent $_SERVER['HTTP_USER_AGENT'];
    if (
    preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
       
    $browser_version=$matched[1];
       
    $browser 'IE';
    } elseif (
    preg_match'|Opera ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
       
    $browser_version=$matched[1];
       
    $browser 'Opera';
    } elseif(
    preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
       
    $browser_version=$matched[1];
       
    $browser 'Firefox';
    } elseif(
    preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
       
    $browser_version=$matched[1];
       
    $browser 'Safari';
    } else {
       
    // browser not recognized!
       
    $browser_version 0;
       
    $browser'Altro';

    Sistema operativo:
    Codice PHP:
    $useragent $_SERVER['HTTP_USER_AGENT'];
    if (
    strstr($useragent,'Win')) {
       
    $os='Windows';
    } else if (
    strstr($useragent,'Mac')) {
       
    $os='Mac';
    } else if (
    strstr($useragent,'Linux')) {
       
    $os='Linux';
    } else if (
    strstr($useragent,'Unix')) {
       
    $os='Unix';
    } else {
        
    $os='Altro';
    }
            
    return 
    $os
    Ce l'ho fatta! - ItalianPixel -

  3. #3
    Utente di HTML.it L'avatar di the-bit
    Registrato dal
    Feb 2005
    Messaggi
    543
    Ciao e grazie per il tuo utilissimo intervento.
    Non è che sarebbe possibile anche conoscere la versione del windows utilizzato? Tipo 7, Xp, ecc?
    Grazie.
    "To iterate is human, to recurse, divine." (R.(Heller))

  4. #4
    Allora prova così:
    Codice PHP:
        $osar = array("Windows XP" => "Windows NT 5.1",
                
    "Windows 2000" => "Windows NT 5.0",
                
    "Windows ME" => "Windows NT 4.90",
                
    "Windows 95" => "Win95",
                
    "Windows 98" => "Win98",
                
    "Windows NET" => "Windows NT 5.2",
                    
    "Windows NT" => "WinNT4.0",
                
    "Mac" => "Mac|PPC",    
                
    "Linux" => "Linux",
                
    "FreeBSD" => "FreeBSD",
                
    "SunOS" => "SunOS",
                
    "Irix" => "Irix",
                
    "BeOS" => "BeOS",
                
    "OS/2" => "OS/2",
                
    "AIX" => "AIX",
               )

        foreach(
    $osar as $chiave => $valore){
            if(
    eregi($valore$_SERVER["HTTP_USER_AGENT"])){
                
    $os=$chiave;
            }else{
                
    $os='Altro';
            }
        }
        echo 
    $os
    non testato ma dovrebbe andare.
    Basta che aggiungi quelli che ti interessano, tipo windows 7, 8 o altro.
    Ce l'ho fatta! - ItalianPixel -

  5. #5
    Utente di HTML.it L'avatar di the-bit
    Registrato dal
    Feb 2005
    Messaggi
    543
    Ancora grazie!
    Visto che mancano windows Vista e 7, sarebbero rispettivamente "Windows NT 6.0" e "Windows NT 6.1" ?
    "To iterate is human, to recurse, divine." (R.(Heller))

  6. #6
    Esattamente.
    Ce l'ho fatta! - ItalianPixel -

  7. #7
    C'è un piccolo bug nel codice che non ho testato. Usa questo:
    Codice PHP:
    $osar = array("Windows 7" => "Windows NT 6.1"
                
    "Windows Vista" => "Windows NT 6.0"
                
    "Windows XP" => "Windows NT 5.1"
                
    "Windows 2000" => "Windows NT 5.0"
                
    "Windows ME" => "Windows NT 4.90"
                
    "Windows 95" => "Win95"
                
    "Windows 98" => "Win98"
                
    "Windows NET" => "Windows NT 5.2"
                    
    "Windows NT" => "WinNT4.0"
                
    "Mac" => "Mac|PPC",     
                
    "Linux" => "Linux"
                
    "FreeBSD" => "FreeBSD"
                
    "SunOS" => "SunOS"
                
    "Irix" => "Irix"
                
    "BeOS" => "BeOS"
                
    "OS/2" => "OS/2"
                
    "AIX" => "AIX"
               ) 

        foreach(
    $osar as $chiave => $valore){ 
            if(
    eregi($valore$_SERVER["HTTP_USER_AGENT"])){ 
                
    $os=$chiave
            }
        } 
        if(!isset(
    $os)) $os='Altro';
        echo 
    $os
    Ti ho aggiunto anche vista e 7
    Ce l'ho fatta! - ItalianPixel -

  8. #8
    Utente di HTML.it L'avatar di the-bit
    Registrato dal
    Feb 2005
    Messaggi
    543
    Ancora grazie mille!

    P.s. carino il tuo sito!
    "To iterate is human, to recurse, divine." (R.(Heller))

  9. #9
    Grazie Dacci occhio che magari trovi qualcosa che ti interessa

    p.s. non l'ho fatto per avere visite o pubblicità ma solo per aiutare chi ha bisogno e per ricordarmi io stesso alcune cose che magari uso raramente ma che possono essere utili. Giusto per chiarire
    Ce l'ho fatta! - ItalianPixel -

  10. #10
    Utente di HTML.it L'avatar di the-bit
    Registrato dal
    Feb 2005
    Messaggi
    543
    Mi riferivo al sito che hai nel profilo.
    Comunque molto utile anche il framework che hai in firma. Magari se le uso ti darò una mano su eventuali bugs o migliorie.
    Meglio finirla qui o andiamo offtopic magari ci si sente per PM!
    Ancora grazie per le dritte sull'User Agent.
    "To iterate is human, to recurse, divine." (R.(Heller))

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.