Question about using PHP Browser/OS detection with Database
talia679
Status: Curious
Joined: 13 Apr 2005
Posts: 5
Reply Quote
I have my stats stored in a database and I'd like to create a report to print out the actual browser name instead of something like this: "Mozilla/4.0 (compatible; MSIE 5.01; Windows 95) "

I have this query:

:: Code ::

$q = mysql_query("SELECT browser FROM statstable GROUP BY browser",$connect);


Once I have the results how do I use your function to get the actual browser name? I was thinking like this:

:: Code ::

while ($b = mysql_fetch_array($q))
{
   $browser = browser_detection($b['browser']);
}


Is this right?

Thanks!
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
No, that won't work at all, you have to think about this one a little bit. What you are currently doing is passing browser_detection a non-existent parameter, the useragent string.

This function is designed to output components of the user agent string, not to accept input. The input is always based on the user agent string, so if you want this to work, you have to do some tweaking of the script. It's also designed to run only one time, it sets all its values as static, that's to make it a bit more efficient, so if you have a function that repeatedly requests the browser function, the only time you'll get a meaningful value is the first time.

It's not that hard to modify the script to handle these things, but since it's not something most people want or need, although I can see the utility if you are storing useragent strings in the db, I don't think I could justify changing to support input as well as output.

This script was really designed to do the opposite of what you're doing, handle a single request, then write the information out, either to a db or the requesting page.

In order to do what you want, you'd have to add an input parameter, then do some testing on that, if it's active, use the input useragent string, if not, use the requesting browser useragent string. Then you'd have to have that component return the values you wanted, not super hard to do.
Back to top
talia679
Status: Curious
Joined: 13 Apr 2005
Posts: 5
Reply Quote
I went and looked more closely at the code and saw that what I asked wouldn't work. I did make some tweaks, but haven't gotten anywhere yet, I'll see what I can come up with.

Thanks though.
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
To make that work, you need to add a parameter to this:
:: Code ::
browser_detection( $which_test ) {

change to:

:: Code ::
browser_detection( $which_test, $user_agent ) {

then you need to turn off static variables, remove the:
:: Code ::
if ( !$b_repeat )
{

conditional that wraps the main part of the function, then change this:
:: Code ::
//make navigator user agent string lower case to make sure all versions get caught
$browser_user_agent = strtolower( $_SERVER ['HTTP_USER_AGENT'] );

to
:: Code ::
if ( !$user_agent )
{
//make navigator user agent string lower case to make sure all versions get caught
$browser_user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
}
else
{
//make navigator user agent string lower case to make sure all versions get caught
      $browser_user_agent = strtolower( $user_agent );
}


That will enable the script to accept the input data if required.

You might need to make some more changes here and there, I'm not sure, I know you'd need to change at least those parts.
Back to top
talia679
Status: Curious
Joined: 13 Apr 2005
Posts: 5
Reply Quote
Sorry it took so long for me to come back. I didn't know you replied ;)

Thanks for this, I did something similar but it's not working. I'm going to test your way and I'll let you know what I come up with.
Back to top
talia679
Status: Curious
Joined: 13 Apr 2005
Posts: 5
Reply Quote
This works great, I forgot to remove the

:: Code ::
if ( !$b_repeat )
{


But other than that my edits were the same as yours.

Thanks again! This is great
Back to top
Display posts from previous:   

All times are GMT - 8 Hours