p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Re: Testing for browser type AND version


Message #1 by "Lawrence" <lkrubner@g...> on Sun, 31 Mar 2002 18:49:56 -0500
From: "Mae Shell" <mae@m...>
>The problem I'm having is that the data contained in $HTTP_USER_AGENT is a
>string variable. Thus, there is no way I can see to split it up into
>meaningful chunks that can be used in an "if" test. Because it's a string, I
>can't use implode(), which is one thing I did try.

You could use explode() on " " , which is to say, on an open space, and then loop through the array
and test for each word. But that would be a terrible way to do things. Better is to use strstr()

$browser = $HTTP_USER_AGENT;
if (strstr("$browser", "MSIE") {
your code here
}




Anyone know of a way to do this, short of using regular expressions?

The only solution I've come up with is something like the following. I
noticed that if I say:

            echo $HTTP_USER_AGENT[0] (or [1] or [2] and so on)

the result is a single character from somewhere in the string (if you count
the first character as [0] and the second as [1] and so on, it is possible
to figure out which letter will be displayed). Thus, if I say:

            echo $HTTP_USER_AGENT;

and the result is:

            Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC)

then the result of:

            echo $HTTP_USER_AGENT[4]

would be:

            l (the fifth character in the string)

So, I thought maybe I could put these together by assigning each character
to its own variable, and then concatenating them together by doing this:

            $zero = $HTTP_USER_AGENT[0]

and so on through the whole string, and then putting several of them
together, like this:

            $together = ($zero, $one, $two, $three);

Which, if this worked, would presumably result in:

            Mozi

Maybe I can use this to get meaningful sections of the $HTTP_USER_AGENT
string to work with. However, this seems awfully complicated and clunky. I'm
looking for a better way!

Thanks,

-Mae


--
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

_ _ MAE R. SHELL - mae@m...
/` \/ `\ http://maeshell.com
\ /
'. .'   "Obstacles are those frightful things you see
\/     when you take your eyes off your goal."
             -Henry Ford

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *












Message #2 by "Lawrence" <lkrubner@g...> on Sun, 31 Mar 2002 19:08:35 -0500
From: "Mae Shell" <mae@m...>
>>>>>>
the result is a single character from somewhere in the string (if you count
the first character as [0] and the second as [1] and so on, it is possible
to figure out which letter will be displayed). Thus, if I say:
            echo $HTTP_USER_AGENT;
and the result is:
            Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC)
then the result of:
            echo $HTTP_USER_AGENT[4]
would be:
            l (the fifth character in the string)
>>>>>>

Uh, by the way, if $HTTP_USER_AGENT gives you the string to test, then just test the whole string.
If you want to find a particular portion, like MSIE, use strstr() as I mentioned before. It returns
false if the string isn't found.

In general this syntax :

$var[2]

is for stepping through arrays. If you use it on a string then PHP steps through the string one at
time, as you found out.





  Return to Index