Miva, Miva Script, Miva Empresa, Miva Mia amd Miva Merchant are registered trademarks of the Miva Corporation
 
Ivo Truxa - truXoft control systems: advanced programming and custom IT solutions home / about / webdesign / Miva / automation / contact

http://mivo.truxoft.com
MIVO!
miva beyond limits

 

MIVA®  SNIPPETS:  Browser Version Sniffer

by Ivo Truxa, 11/11/2000

The following function extracts the name of a browser (User Agent) and its version number from the s.http_user_agent varaible. This is a minimized, but working version:

<MvFUNCTION NAME="getUA" PARAMETERS="ua">
 <MvASSIGN NAME="l.knownUA" VALUE="MSIE|Opera|MuscatFerret|Mozilla">
 <MvWHILE EXPR="{l.nr LT 4}">
   <MvASSIGN NAME="l.nr" VALUE="{l.nr+1}">
   <MvASSIGN NAME="l.pos" VALUE="{gettoken(l.knownUA,'|',l.nr) ECIN l.ua}">
   <MvIF EXPR="{l.pos AND 1*substring(l.ua,l.pos+2,6)}">
    <MvFUNCRETURN VALUE="{gettoken(l.knownUA,'|',l.nr) $ '|' $ 1*substring(l.ua,l.pos+2,6)}">
   </MvIF>
 </MvWHILE>
</MvFUNCTION>

After adding little bit more flexibility and modifying the agent name in the return string:

<MvFUNCTION NAME="getUA" PARAMETERS="ua">
 <MvASSIGN NAME="l.knownUA" VALUE="MSIE|Opera|MuscatFerret|Mozilla">
 <MvASSIGN NAME="l.returnUA" VALUE="MS Explorer|Opera|Muscat Ferret|Netscape">
 <MvWHILE EXPR="{l.nr LE (len(l.knownUA)-len(glosub(l.knownUA,'|','')))}">
   <MvASSIGN NAME="l.nr" VALUE="{l.nr+1}">
   <MvASSIGN NAME="l.pos" VALUE="{gettoken(l.knownUA,'|',l.nr) ECIN l.ua}">
   <MvASSIGN NAME="l.ver" VALUE="{1*glosub(substring(l.ua,l.pos+1,6),'/',' ')}">
   <MvIF EXPR="{l.pos AND l.ver}">
    <MvFUNCRETURN VALUE="{gettoken(l.returnUA,'|',l.nr) $ '|' $ l.ver}">
   </MvIF>
 </MvWHILE>
</MvFUNCTION>

If you want to add a new browser type, just add it into the l.knownUA string, but remember that Mozilla allways has to stay the last one!

If you wish that the function returns another name the one used in the HTTP User Agent, then add it in the respective place into the secon l.returnUA string. In the other case, add an identical string as in l.knownUA. The getUA function returns a string in the following format: name|version and an empty string when there was no known User Agent recognized or the s.http_user_agent was empty.

UPDATE: Bill Guindon made a fix for recognizing correctly the following UA string:
Mozilla/4.02 (compatible; MSIE 5.0; Windows NT 4.0) Opera 4.02 [en]
The following change (changed order) in the l.knownUA and l.returnUA will fix it:
<MvASSIGN NAME="l.knownUA" VALUE="MuscatFerret|Opera|AOL|MSIE|Mozilla">
<MvASSIGN NAME="l.returnUA" VALUE="Muscat Ferret|Opera|AOL|MS Explorer|Netscape">

Originally I put the MSIE string as the first one. That was for performance reasons: big part of browsers are different versions of MSIE and therefore avoiding to parse the less probable browser strings, speeds up the function, but Bill is right that some exotic agents may put a compatibility string first - it is up to you to decide which one you want to trap.

Both functions return identical results (with the exception of the changed agent names) for the following UA strings:

User AgentUAVersion
   
CCBot/1.0 (+http://www.commoncrawl.org/bot.html)     
Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)  MS Explorer  5.5 
Mozilla/2.0 (compatible; MSIE 3.02; Windows 3.1)  MS Explorer  3.02 
Mozilla/2.02 (Macintosh; I; 68K)  Netscape  2.02 
Mozilla/3.04Gold (Win95; I)  Netscape  3.04 
Mozilla/4.0 (Windows 4.10;US) Opera 3.60 [en]  Opera  3.6 
Mozilla/4.0 (compatible; MSIE 4.01; AOL 4.0; Windows 95)  AOL  4 
Mozilla/4.0 (compatible; MSIE 4.01; AOL 4.0; Windows 98)  AOL  4 
Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)  MS Explorer  4.01 
Mozilla/4.0 (compatible; MSIE 4.01; Windows 95; CHOI99095)  MS Explorer  4.01 
Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)  MS Explorer  4.01 
Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)  MS Explorer  4.01 
Mozilla/4.0 (compatible; MSIE 4.0; Windows 95)  MS Explorer  4 
Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)  MS Explorer  5.01 
Mozilla/4.0 (compatible; MSIE 5.01; Windows 98; TUCOWS Network)  MS Explorer  5.01 
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)  MS Explorer  5.01 
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; AIRF)  MS Explorer  5.01 
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)  MS Explorer  5.01 
Mozilla/4.0 (compatible; MSIE 5.0; AOL 4.0; Windows 98; CNETHomeBuild03171999)  AOL  4 
Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt; AltaVista 1.01.01)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt; TUCOWS)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; CNETHomeBuild03171999)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; CNETHomeBuild051099)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)  MS Explorer  5 
Mozilla/4.0 (compatible; MSIE 5.5; Windows 95; AltaVista 1.01.01)  MS Explorer  5.5 
Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)  MS Explorer  5.5 
Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; CNETHomeBuild051099)  MS Explorer  5.5 
Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; AIRF)  MS Explorer  5.5 
Mozilla/4.0 (compatible; MuscatFerret/2.0; http://www.webtop.com/)  Muscat Ferret  2 
Mozilla/4.0 (compatible; Opera/3.0; Windows 4.10) 3.51 [en]  Opera  3 
Mozilla/4.04 [en] (Win95; I)  Netscape  4.04 
Mozilla/4.05 (Macintosh; I; PPC, Nav)  Netscape  4.05 
Mozilla/4.05 [en] (Win95; I ;Nav)  Netscape  4.05 
Mozilla/4.05 [en] (Win95; I)  Netscape  4.05 
Mozilla/4.07 [en] (WinNT; I)  Netscape  4.07 
Mozilla/4.08 [en] (Win98; U ;Nav)  Netscape  4.08 
Mozilla/4.5 [en] (Win98; I)  Netscape  4.5 
Mozilla/4.5 [en] (Win98; U)  Netscape  4.5 
Mozilla/4.7 [en] (Win95; I)  Netscape  4.7 
Mozilla/4.7 [en] (Win95; U)  Netscape  4.7 
Mozilla/4.72 [en] (Win95; I)  Netscape  4.72 
Mozilla/4.75 [en] (Win98; U)  Netscape  4.75 
Mozilla/5.0 (Windows NT 4.0; U) Opera 4.02 [en]  Opera  4.02 

 

top

   

Miva and some other terms used on this page are registerd trademarks of the Miva Corporation
copyright  truXoft  © 1997-2008