Forum Moderators: coopster
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
<?
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.
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();
}