p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Continuing the browser version/type quest


Message #1 by Mae Shell <mae@m...> on Mon, 01 Apr 2002 16:55:48 -0500
Hi, everyone! I'm continuing to look for the best way to test for a user's
browser type and version. I have one quick question.

If I use the following code:

    if ($HTTP_USER_AGENT, "MSIE") {
         echo "Yes.";
         } else {
         echo "No.";
    }

The result would be that, if the browser is Explorer, I'll get a Yes and if
it isn't, I'll get a no. The test checks to see if "MSIE" is anywhere in the
$HTTP_USER_AGENT string.

Is there a way to ask if "MSIE" *isn't* in the string? In other words, to
say, "If MSIE is not in this string, then do this."

I tried the following, which didn't work:

    if ($HTTP_USER_AGENT, ! "MSIE") {
        echo "Other.";
        } else {
        echo "Internet explorer.";
    }

But, I got an error. Is there any way to do this?

Thanks,

-Mae

Message #2 by "Juliet May" <jmay@s...> on Mon, 1 Apr 2002 15:46:25 -0700
You would use your first example. if "MSIE" is not in the string then only
the commands after the else statement will run. So instead of saying "yes"
if "MSIE" is in the string you would echo "Internet Explorer". For your else
statement you would echo "other" or any other command you wanted implemented
if the browser is not "MSIE".

Julie

----- Original Message -----
From: "Mae Shell" <mae@m...>
To: "beginning php" <beginning_php@p...>
Sent: Monday, April 01, 2002 2:55 PM
Subject: [beginning_php] Continuing the browser version/type quest


> Hi, everyone! I'm continuing to look for the best way to test for a user's
> browser type and version. I have one quick question.
>
> If I use the following code:
>
>     if ($HTTP_USER_AGENT, "MSIE") {
>          echo "Yes.";
>          } else {
>          echo "No.";
>     }
>
> The result would be that, if the browser is Explorer, I'll get a Yes and
if
> it isn't, I'll get a no. The test checks to see if "MSIE" is anywhere in
the
> $HTTP_USER_AGENT string.
>
> Is there a way to ask if "MSIE" *isn't* in the string? In other words, to
> say, "If MSIE is not in this string, then do this."
>
> I tried the following, which didn't work:
>
>     if ($HTTP_USER_AGENT, ! "MSIE") {
>         echo "Other.";
>         } else {
>         echo "Internet explorer.";
>     }
>
> But, I got an error. Is there any way to do this?
>
> Thanks,
>
> -Mae
>
>
>

Message #3 by "Nikolai Devereaux" <yomama@u...> on Mon, 1 Apr 2002 15:27:52 -0800
You can use a few different functions to accomplish this.  ereg(),
preg_match(), etc... but I'd recomment strstr, since it saves you the
overhead of computing regular expressions patterns, and you don't have to
worry that your $needle will be parsed as a pattern that can match some part
of your $haystack.


// This returns a boolean true if $needle is NOT found
// within $haystack, and a boolean false if it IS.
function notIn($needle, $haystack)
{
   // if you have php 4
   return (false === strstr($haystack, $needle));

   // if you are living in the distant past!
   return !(strstr($haystack, $needle));
}


Your code then becomes

if( notIn("MSIE", $HTTP_USER_AGENT) )
{
  ...
}


take care,

nik

Message #4 by "Nikolai Devereaux" <yomama@u...> on Mon, 1 Apr 2002 15:31:24 -0800
P.S.  Please tell me that this is pseudocode...

>     if ($HTTP_USER_AGENT, "MSIE") {

It looks like a parse error to me. =D
Message #5 by "Krister" <krister@m...> on Tue, 2 Apr 2002 14:18:09 +0200
Hi, I dont know any place to get browser infos, but here few from my
computers:

Mac
explorer:
Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC)
netscape:
Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:0.9.4.1) Gecko/20020315
Netscape6/6.2.2
OmniWeb:
Mozilla/4.5 (compatible; OmniWeb/4.0.6; Mac_PowerPC)

Win
explorer:
Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Netscape:
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:0.9.4) Gecko/20011128
Netscape6/6.2.1
opera:
Opera/5.12 (Windows 2000; U)  [da]

Try this code

if ( strstr($HTTP_USER_AGENT, "MSIE") != 0) {      // 0=false 1=true
         echo "Other.";
         } else {
         echo "Internet explorer.";
     }

Krister

----- Original Message -----
From: "Mae Shell" <mae@m...>
To: "beginning php" <beginning_php@p...>
Sent: Monday, April 01, 2002 11:55 PM
Subject: [beginning_php] Continuing the browser version/type quest


> Hi, everyone! I'm continuing to look for the best way to test for a user's
> browser type and version. I have one quick question.
>
> If I use the following code:
>
>     if ($HTTP_USER_AGENT, "MSIE") {
>          echo "Yes.";
>          } else {
>          echo "No.";
>     }
>
> The result would be that, if the browser is Explorer, I'll get a Yes and
if
> it isn't, I'll get a no. The test checks to see if "MSIE" is anywhere in
the
> $HTTP_USER_AGENT string.
>
> Is there a way to ask if "MSIE" *isn't* in the string? In other words, to
> say, "If MSIE is not in this string, then do this."
>
> I tried the following, which didn't work:
>
>     if ($HTTP_USER_AGENT, ! "MSIE") {
>         echo "Other.";
>         } else {
>         echo "Internet explorer.";
>     }
>
> But, I got an error. Is there any way to do this?
>
> Thanks,
>
> -Mae
>
>
>

Message #6 by "Lawrence" <lkrubner@g...> on Thu, 4 Apr 2002 14:54:38 -0500
>>>
----- Original Message -----
From: "Krister" <krister@m...>
Try this code

if ( strstr($HTTP_USER_AGENT, "MSIE") != 0) {      // 0=false 1=true
         echo "Other.";
         } else {
         echo "Internet explorer.";
     }
>>>

The test in the if clause, !=0, is a little redundant since strstr() returns false if the string is
not found.


  Return to Index