Page 1 of 1

PHP Set/Detect Gecko Revision via preg_match to variable?

Posted: Wed Jan 25, 2006 9:08 pm
by JAB Creations
I'm trying to detect the version of the Gecko engine. If a user has Firefox 1.0.7 the variable when printed would output "1.7.12" or "1.8" for Firefox 1.5.

I know it's supposed to look something like this...
$gecko = preg_match('/rv[: ]?([0-9a-z.+]+)/i');
...plus I have to refer to the useragent.

I'd like to set the useragent to this variable...
$useragent = getenv("HTTP_USER_AGENT");
...and then detect the gecko version and set it to $gecko variable.

Pre-built scripts come with no competent manuals and require more hard core PHP know how and this solution is truly all I need right now.

That way I can use greater or less then operators to serve patched code for older versions of Gecko using less then math operators. If Gecko is less then 1.8...
if ($gecko<1.8) {echo 'Gecko is less then 1.8, send certain code here.';}
John

Posted: Thu Jan 26, 2006 6:16 am
by Jenk
I don't have access to a PHP parser atm, so I can't help with the pattern just now, but the following snippet should help you on your way:

Code: Select all

<?php

$useragent = $_SERVER['HTTP_USER_AGENT'];

if ((preg_match(/*pattern goes here*/, $useragent, $matches)) > 0) {
    $gecko = floatval($matches[0]);

    if ($gecko < 1. {
        echo 'Gecko version is less than 1.8, send certain code here';
    }
}
?>

Posted: Thu Jan 26, 2006 1:32 pm
by JAB Creations
Thanks Jenk, I added this...
if(preg_match('/gecko\/([0-9]+)/i', $useragent, $matches))
But when I added this at the end of that line...
> 0)

I got a parse error. Without that last bit while it does not create an error it always thinks Gecko is less then 1.8.

John

Posted: Thu Jan 26, 2006 1:46 pm
by Chris Corbyn
JAB Creations wrote:Thanks Jenk, I added this...
if(preg_match('/gecko\/([0-9]+)/i', $useragent, $matches))
But when I added this at the end of that line...
> 0)
I got a parse error. Without that last bit while it does not create an error it always thinks Gecko is less then 1.8.

John
Considered echoing the value out that it's finding? My guess is that 1.9 and 1.8 and 1.8.5 etc will all show as less than 1.8 ;)

Code: Select all

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];

if (preg_match('@gecko/([0-9]+(?:\.[0-9]+)?)@', $useragent, $matches))
{
    $gecko = $matches[1];

    if ($gecko < 1.
    {
        echo 'Gecko version is less than 1.8, send certain code here';
    }
}
?>

Posted: Thu Jan 26, 2006 2:18 pm
by Jenk

Code: Select all

<?php

$useragent = $_SERVER['HTTP_USER_AGENT'];

if ((preg_match('/rv:(\d\.\d)+\)/i', $useragent, $matches)) > 0) {
    $gecko = floatval($matches[1]);

    if ($gecko < 1. {
        echo 'Gecko version is less than 1.8, send certain code here';
    } else {
        echo 'Gecko 1.8 or above detected!';
    }
}
?>

Kinda working!

Posted: Thu Jan 26, 2006 8:39 pm
by JAB Creations
Well there are two types of revisions this will not work with...

Alpha and minor revision patches.

Minor Revision...
rv:1.7.5
Alpha example...
rv:1.9a1
I can switch from making the script work for minor revisions but not normal main revisions (so 1.7.5 would work but 1.7 would not) but adding another ".\d".

So the tricky part I don't know is how to detect a minor version (so we can also output the full Gecko version and also deal accordingly with alpha and beta releases) by conditionally detecting these extra characters?

Code: Select all

<?php
//
// Working examples  commented at the end of echos.
// Using Chris Pederick's User Agent Switch for testing
//
// Does not detect with "alpha" a (rv:1.9a1) --> "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20060120 Firefox/1.6a1"
// Does not detect with minor version (rv:1.7.5)--> "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0"
//

$useragent = $_SERVER['HTTP_USER_AGENT'];

if ((preg_match('/rv:(\d\.\d)+\)/i', $useragent, $matches)) > 0)
{
$gecko = floatval($matches[1]);

if     ($gecko < 1.8)  {echo 'Less than 1.8';}     // "rv:1.4" works
elseif ($gecko > 1.8)  {echo 'Greater then 1.8';}
elseif ($gecko == 1.8) {echo 'Is 1.8';}
else                   {echo 'NOT equal, less, or greater then?';}
}
echo ' <br />';
echo ' <br />';
echo 'Detected Gecko  ';
echo $gecko;
?>
Thanks for your continued help folks! :-)

Posted: Fri Jan 27, 2006 3:32 am
by Jenk
You can do a string compare instead of a float compare, but it will mean you can't just do < or > and will have to list all the available options.

Try this pattern in the preg_replace:

Code: Select all

preg_replace('/rv\:([0-9a-z\.]+)\)/i', $useragent, $matches)
and remove the floatval function leaving just $gecko = $matches[1];

Posted: Fri Jan 27, 2006 6:41 am
by feyd

Posted: Fri Jan 27, 2006 6:54 am
by Jenk
I'll take a seat.
















:P

Posted: Fri Jan 27, 2006 12:23 pm
by JAB Creations
What I'm trying to understand right now is what...

Code: Select all

0-9a-z
and

Code: Select all

\d\.\d)
are referenced as? Filters? Strings? The rest of the script makes sense to me. If I can reference these correctly maybe I could make more effective suggestions when I'm looking this stuff up?

John

Posted: Fri Jan 27, 2006 12:45 pm
by feyd
check out the regex board (on this server) all kindsa stuff for this is explained.. again and again. :)