Forum Moderators: coopster

Message Too Old, No Replies

PHP false negative on comparing array to variable?

         

JAB Creations

4:27 pm on May 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Like always, please stay on topic. I'm testing this locally and it will never see the light of day on a live site. Using browser useragents is a method I understand already being used to learn a method I do not yet understand.

Below is my attempt at comparing a string ($useragent) to any of the values in an array ($browser).

I'm getting a false negative. This means no matter if I'm not spoofing and match, spoofing and match, not matching, not matching and spoofing, I always get 403 (false negative). This needs to respond as 200 for MSIE, Opera, or Firefox, and 403 to any other useragent. I'm using the useragent switcher extension for Firefox to spoof useragents (such as Google) and also testing this with SeaMonkey for a true mismatch (without having to spoof).

So all I need is some sort of code fix. Here is what I have right now...

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser=array('MSIE','Opera','Firefox');

if ( in_array($useragent, $browser) )
{
echo '200<br />';
echo $useragent;
}
else
{
header("HTTP/1.0 403");
echo '403<br />';
echo $useragent;
die();
}
?>

- John

grandpa

7:35 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I toyed with this for a little while, and I ended up using a class (phpsniff) to return the browser long name (ie, Netscape, Firefox..). The class returns this name in lower case.

<?
include ("phpsniff.inc");
$mysniff = new phpSniff();
$mysniff->phpSniff($UA='',$settings = false);
$class_vars = get_class_vars(get_class($mysniff));
foreach ($class_vars as $name => $value) {
if ($name == '_browser_info') {
while(list($key,$val) = each($mysniff->_browser_info)) {
if ($key == 'long_name') $useragent = $val;
}
}
}

$browser=array('opera','firefox','msie');

if ( in_array($useragent, $browser) )
{
echo '200<br />';
echo $useragent;
}
else
{
header("HTTP/1.0 403");
echo '403<br />';
echo $useragent;
die();
}
?>

The problem is that $useragent (in the original example) is getting assigned a value like Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.5) Gecko/20051012 Netscape/8.0.4, and the needle it too big for the haystack. You might pore over the phpsniff class to see how the long name was extracted, or, you could take the easy route and just use it.

p1lky

7:52 am on May 21, 2006 (gmt 0)

10+ Year Member



if ( in_array($useragent, $browser) ) checks if $browser[]==$useragent. It doesn't because there's always extra bumph in $useragent

foreach ($browser as $value)
{
if (eregi($value, $useragent))
{
$matchit=1;
}
}
if ($matchit==1)
{
echo '200<br />';
echo $useragent;
}
else
{
header("HTTP/1.0 403");
echo '403<br />';
echo $useragent;
die();
}

grandpa

8:08 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Someday (I promise) I will actually write some code that uses ereg() or eregi() functions, then I might remember them :)