[PHP] Browser Sniff (Code Review Encouraged)

2002-02-18 Thread Robert Miller

Hello,

Below is code I hacked up to read a user agent string (i.e. Mozilla/5.0 (Windows; U; 
Windows NT 5.0; en-US; rv:0.9.8) Gecko/20020204
) and return several items including:
- the browser name,
- the browser version,
- the browser's major version number, and
- user definable compatibility listings.

This post is attached so that others may benefit. DISCLAIMER: I've been working with 
PHP for a few months... about as long as I've ever been programming - feel free to 
trust my brain at your own risk. Your thoughts and suggestions are welcome. :-)

ASSUMING CORRECTIONS ARE MADE DUE TO MAILING-LIST COMMENTS, I WILL REPOST THIS CODE.

Cheers, Rob

P.S. Is this the correct forum for tossing code around?



Now for the code...




WEB PAGE: (*.php )   -- I removed all the HTML for readability.

?
require (sniff.inc);
require (ua.inc);

$uasniff = sniff ($HTTP_USER_AGENT);
$uaraw = $uasniff[raw];
$uaname = $uasniff[name];
$uaversion = $uasniff[version];
$uamajor = $uasniff[major];
$uatest = ua ($uaname, $uamajor);

echo $uaraw;
echo $uaname;
echo $uaversion;

if ($uatest) { echo compatible; }
else { echo not compatible; }

?





REQUIRED FILE: (sniff.inc )

?
function sniff($ua) {
if ( preg_match(/Mozilla/,$ua) == true ) {
if ( preg_match(/MSIE/,$ua) == true  preg_match(/Opera/,$ua) == 
false ) {
preg_match(/(?=MSIE )\S{3}/,$ua, $matches);
$name = Internet Explorer;
$version = $matches[0];
}
elseif ( preg_match(/Netscape/,$ua) == true ) {
preg_match(/(?=Netscape6\/)\S{3,5}/,$ua, $matches);
$name = Netscape;
$version = $matches[0];
}
elseif ( preg_match(/Opera/,$ua) == true ) {
preg_match(/(?=Opera )\S{3,5}/,$ua, $matches);
$name = Opera;
$version = $matches[0];
}
elseif ( preg_match(/Gecko/,$ua) == true ) {
preg_match(/(?=rv:)\S{3,5}/,$ua, $matches);
$name = Mozilla;
$version = $matches[0];
}
else {
preg_match(/(?=Mozilla\/)\S{3,5}/,$ua, $matches);
$name = Netscape;
$version = $matches[0];
}
}
elseif ( preg_match(/Lynx/,$ua) == true ) {
preg_match(/(?=Lynx\/)\S{3,11}/,$ua, $matches);
$name = Lynx;
$version = $matches[0];
}
if ( $version == true  ) {
preg_match(/[0-9]/, $version, $matches);
$major = $matches[0];
}
return array ( 'name' = $name , 'version' = $version, 'major' = $major, 
'raw' = $ua );
}
?





REQUIRED FILE: (ua.inc )

?
function ua($name, $major) {
$uaarray = array (
Internet Explorer = array (
4 = true,
5 = true,
6 = true
),
Netscape = array (
4 = true,
6 = true
),
Opera = array (
5 = true,
6 = true
),
Mozilla = array (
0 = true
),
);
if ($uaarray[$name][$major]) {
return 1;
}
}
?



--
Robert J. Miller
Internet Support Specialist
Department of Government Services and Lands
P.O. Box 8700, St. John's, NF., A1B-4J6
(709) 729-4520 (phone)
(709) 729-4151 (facsimile)
(709) 778-8746 (pager)

http://www.gov.nf.ca/gsl/
mailto:[EMAIL PROTECTED]
--
Simple things should be simple and hard things
should be possible.
--


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Browser Sniff (Code Review Encouraged)

2002-02-18 Thread Erik Price

Just a quick question -- the outsidemost if/elseif sequence of the 
function sniff() doesn't seem to have an else (it ends with elseif 
and then does another if, before its return).  Did you mean to 
include an else in that?


Erik




On Monday, February 18, 2002, at 04:37  PM, Robert Miller wrote:

 Hello,

 Below is code I hacked up to read a user agent string (i.e. Mozilla/5.0 
 (Windows; U; Windows NT 5.0; en-US; rv:0.9.8) Gecko/20020204
 ) and return several items including:
 - the browser name,
 - the browser version,
 - the browser's major version number, and
 - user definable compatibility listings.

 This post is attached so that others may benefit. DISCLAIMER: I've been 
 working with PHP for a few months... about as long as I've ever been 
 programming - feel free to trust my brain at your own risk. Your 
 thoughts and suggestions are welcome. :-)

 ASSUMING CORRECTIONS ARE MADE DUE TO MAILING-LIST COMMENTS, I WILL 
 REPOST THIS CODE.

 Cheers, Rob

 P.S. Is this the correct forum for tossing code around?



 Now for the code...




 WEB PAGE: (*.php )   -- I removed all the HTML for readability.

 ?
 require (sniff.inc);
 require (ua.inc);

 $uasniff = sniff ($HTTP_USER_AGENT);
 $uaraw = $uasniff[raw];
 $uaname = $uasniff[name];
 $uaversion = $uasniff[version];
 $uamajor = $uasniff[major];
 $uatest = ua ($uaname, $uamajor);

 echo $uaraw;
 echo $uaname;
 echo $uaversion;

 if ($uatest) { echo compatible; }
 else { echo not compatible; }

 ?





 REQUIRED FILE: (sniff.inc )

 ?
 function sniff($ua) {
   if ( preg_match(/Mozilla/,$ua) == true ) {
   if ( preg_match(/MSIE/,$ua) == true  
 preg_match(/Opera/,$ua) == false ) {
   preg_match(/(?=MSIE )\S{3}/,$ua, $matches);
   $name = Internet Explorer;
   $version = $matches[0];
   }
   elseif ( preg_match(/Netscape/,$ua) == true ) {
   preg_match(/(?=Netscape6\/)\S{3,5}/,$ua, $matches);
   $name = Netscape;
   $version = $matches[0];
   }
   elseif ( preg_match(/Opera/,$ua) == true ) {
   preg_match(/(?=Opera )\S{3,5}/,$ua, $matches);
   $name = Opera;
   $version = $matches[0];
   }
   elseif ( preg_match(/Gecko/,$ua) == true ) {
   preg_match(/(?=rv:)\S{3,5}/,$ua, $matches);
   $name = Mozilla;
   $version = $matches[0];
   }
   else {
   preg_match(/(?=Mozilla\/)\S{3,5}/,$ua, $matches);
   $name = Netscape;
   $version = $matches[0];
   }
   }
   elseif ( preg_match(/Lynx/,$ua) == true ) {
   preg_match(/(?=Lynx\/)\S{3,11}/,$ua, $matches);
   $name = Lynx;
   $version = $matches[0];
   }
   if ( $version == true  ) {
   preg_match(/[0-9]/, $version, $matches);
   $major = $matches[0];
   }
   return array ( 'name' = $name , 'version' = $version, 'major' = 
 $major, 'raw' = $ua );
 }
 ?





 REQUIRED FILE: (ua.inc )

 ?
 function ua($name, $major) {
   $uaarray = array (
   Internet Explorer = array (
   4 = true,
   5 = true,
   6 = true
   ),
   Netscape = array (
   4 = true,
   6 = true
   ),
   Opera = array (
   5 = true,
   6 = true
   ),
   Mozilla = array (
   0 = true
   ),
   );
   if ($uaarray[$name][$major]) {
   return 1;
   }
 }
 ?



 --
 Robert J. Miller
 Internet Support Specialist
 Department of Government Services and Lands
 P.O. Box 8700, St. John's, NF., A1B-4J6
 (709) 729-4520 (phone)
 (709) 729-4151 (facsimile)
 (709) 778-8746 (pager)

 http://www.gov.nf.ca/gsl/
 mailto:[EMAIL PROTECTED]
 --
 Simple things should be simple and hard things
 should be possible.
 --


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php









Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php