Page 1 of 2

Comparing array to variable?

Posted: Sat May 20, 2006 9:58 am
by JAB Creations
I'm trying to compare an array to a variable. I know how to compare useragents so I'm using that in my example below. Lets say I want to kill the connection if the user isn't using Opera or Internet Explorer (again just an example that I can understand). I'm using Firefox with the useragent switcher so no matter how I'm testing this as long as I have use == operator I still detect as false (even though I may have the UA in Firefox set to MSIE or Opera).

So all I'm doing is trying to compare my the values in the array and if any of them partially match the $useragent variable I want to echo 200.
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser =array(''MSIE','Opera');

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

Posted: Sat May 20, 2006 10:10 am
by bdlang
Use in_array(), e.g.

Code: Select all

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

if ( in_array($useragent, $browser) )
{
   // matches
}
Note the PHP tags caught a syntax error, you had an extra single quote in your array definition.

Posted: Sat May 20, 2006 10:17 am
by JAB Creations
bdlang, I got a false negative from your result (meaning if it matchs it still ignores it and 403s it).

I tried the following and got a false positive...
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser =array('MSIE','Opera','Firefox');

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

Posted: Sat May 20, 2006 10:33 am
by timvw
JAB Creations wrote:I tried the following and got a false positive...

Code: Select all

<?php
$browser =array('MSIE','Opera','Firefox');
if (in_array('Firefox', $browser))
{
 echo '200';
}
?>
and where is the false positive? Your code doesn't even compare with the $_SERVER['HTTP_USER_AGENT'] value.. (Btw, be aware of the fact that this string does not always exists.. and thus can generate warnings if you try to access it..)

Posted: Sat May 20, 2006 10:39 am
by JAB Creations
I'm getting false positives from spoofing with...
http://chrispederick.com/work/useragentswitcher/

...and by using browsers not in the array (such as SeaMonkey).

That is how I'm testing this out since all we're doing is echoing 200 if the useragent string matchs.

Yeah, I'm not the best PHP coder. However testing say SeaMonkey compared to Firefox should echo 200 versus not echoing 200 (with SeaMonkey). It currently echos 200 for all browsers/spoofs which is the false positive.

Re: Comparing array to variable?

Posted: Sat May 20, 2006 10:48 am
by timvw
JAB Creations wrote:I'm trying to compare an array to a variable.
This is the question we've answered...

That you're getting false postives is simply not true since the code you showed us didn't even use $_SERVER['HTTP_USER_AGENT']. All it did was verify if 'Firefox' is in an array with 'MSIE', 'Opera' and 'Firefox'.



(Apparently you're trying to do something with the user-agent header.. So you first might want to read http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html so you understand that user-agents SHOULD but not MUST send this header in their requests... And explains why it's possible that $_SERVER['HTTP_USER_AGENT'] doesn't exist at all... And as you've already experienced, this string can be easily modified and thus become completely unreliable.)

Posted: Sat May 20, 2006 10:56 am
by aerodromoi
JAB Creations wrote:I'm getting false positives from spoofing with...
http://chrispederick.com/work/useragentswitcher/

...and by using browsers not in the array (such as SeaMonkey).

That is how I'm testing this out since all we're doing is echoing 200 if the useragent string matchs.

Yeah, I'm not the best PHP coder. However testing say SeaMonkey compared to Firefox should echo 200 versus not echoing 200 (with SeaMonkey). It currently echos 200 for all browsers/spoofs which is the false positive.
A user agent string typically looks like that: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)
In_array won't solve this problem as it does not tell you whether the user agent string contains one of the strings in the array. You might want to use a loop and ereg() for that.

It's not really my cup of tea - but sending out a 403 is the fastest way to loose visitors. If you really want to check out whether all your visitors have seen a correct version of your website, you might want to use the mail() function to keep track of all those browsers which are not on your whitelist.

aerodromoi

Posted: Sat May 20, 2006 11:06 am
by timvw
aerodromoi wrote:If you really want to check out whether all your visitors have seen a correct version of your website, you might want to use the mail() function to keep track of all those browsers which are not on your whitelist.
Imho, if you develop a standards compliant website all users get to see a correct version.. I find it odd that it took more than 5 years before most webdevelopers understood that the javascript user-agent detection didn't work.. And i'm at least surprised to see it happen again... Since the retard behaviour of webdevelopers was the reason why spoofing the user-agent string become so easily accesible...

Posted: Sat May 20, 2006 11:13 am
by aerodromoi
timvw wrote: Imho, if you develop a standards compliant website all users get to see a correct version..
Then why am I using css hacks and a javascript workaround for a (w3c compliant) website?
The answer is to be found in the $browser array :)

aerodromoi

Posted: Sat May 20, 2006 11:17 am
by JAB Creations
the code you showed us didn't even use $_SERVER['HTTP_USER_AGENT']
$useragent = $_SERVER['HTTP_USER_AGENT'];
You didn't even quote something I posted...that or there was some miscommunication?

Like my signature says I'm sure I want to do this. Just because I'm asking about something does not mean in any way it'll make it's way to a live site. I'm just trying to learn this stuff. I can always test to see if useragent claims are true through code to follow up later (but thats not what we're discussing). I want to keep this small and simple because thats exactly what my understanding of PHP is right now.

Anyway here is what I have now. I'm still getting a false negative.
<?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();
}
?>

Posted: Sat May 20, 2006 11:34 am
by aerodromoi
aerodromoi wrote: A user agent string typically looks like that: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)
In_array won't solve this problem as it does not tell you whether the user agent string contains one of the strings in the array. You might want to use a loop and ereg() for that.
What I'm trying to say is:

You're checking whether your $browser array contains a string like "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)". This won't work.

aerodromoi

ps: I'm not double-posting. I'm repeating myself 8)

Posted: Sat May 20, 2006 2:15 pm
by bdlang
8O :oops:

Damn, and I quickly typed that up this morning as I was waiting for my girlfriend to get ready, and immediately logged off and walked out the door right afterward. Sorry, my bad, the code I provide wouldn't get you anywhere. in_array() is a good method to find if a string exists within an array (which essentially my overly inaccurate speed reading capabilities led me to believe was your problem), but your USER_AGENT value would NOT match at all (which all these posts have attested to).

At any rate, without having to reinvent the wheel, take a look at this set of free PHP scripts I found that I'm sure you can adapt to your needs. Good luck.

Posted: Sat May 20, 2006 2:52 pm
by JAB Creations
Gah...

I'm not trying to detect various browsers or useragents. I'm just using that method because I know how to use it and test it properly.

It's being able to detect whether the part of the useragent exists (from the array) compared to the string (the useragent).

Honestly, I titled this thread comparing a string to a variable, not browser detection. I truly appreciate the help but if we can't stay focused on the topic at hand I see no point to be had.

If someone has a different method other then browser matching fine, I just want to learn how to compare an array to variable! :roll:

Posted: Sat May 20, 2006 3:12 pm
by bdlang
JAB Creations wrote: It's being able to detect whether the part of the useragent exists (from the array) compared to the string (the useragent).

Honestly, I titled this thread comparing a string to a variable, not browser detection. I truly appreciate the help but if we can't stay focused on the topic at hand I see no point to be had.

If someone has a different method other then browser matching fine, I just want to learn how to compare an array to variable! :roll:
Again, all apologies for not reading the thread completely enough. One of those days, I guess.

My problem with your post, however, is that you seem to contradict yourself. You say you want to compare a string to a variable, but at the same time you want to attempt to compare the USER_AGENT string to a simple value like 'Mozilla'. As others mentioned, this isn't going to work.

The PHP manual has plenty of good examples of in_array() usage, as I linked to in the first line of my first post!

Posted: Sat May 20, 2006 3:27 pm
by timvw
JAB Creations wrote: You didn't even quote something I posted...that or there was some miscommunication?
Here is the post i was referring to...
AB Creations - Sat May 20, 2006 4:17 pm

I tried the following and got a false positive...

$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser =array('MSIE','Opera','Firefox');
if (in_array('Firefox', $browser))
{
echo '200';
}
So i'll repeat my question: How can get you get false positives with that code???

JAB Creations wrote: LI'm just trying to learn this stuff.
That is exactly why i referred to the RFC which explains how the user-agent string looks like...
JAB Creations wrote: Anyway here is what I have now. I'm still getting a false negative.
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser=array('MSIE','Opera','Firefox');

if ( in_array($useragent, $browser) )
{
echo '200<br />';
echo $useragent;
}
Since you're trying to learning it, i don't understand why you didn't take the time to read the section in the RFC since it clearly shows that the $_SERVER['HTTP_USER_AGENT'] string does not simply contain 'MSIE', 'Opera' or 'Firefox' (well, they shouldn't anyway).