Jump to content


Photo

Detecting a users browser with PHP?


  • Please log in to reply
20 replies to this topic

#1 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 05 July 2006 - 09:42 PM

How can I detect a users browser type and version? (e.g. Internet Explorer 6.2)
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#2 birdie

birdie
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationBirmingham UK

Posted 05 July 2006 - 09:48 PM

All info like that are stored in the $_SERVER variable.

$_SERVER[HTTP_USER_AGENT] == users browser

Hope that helped ;D

#3 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 05 July 2006 - 10:01 PM

Neat, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4".

But how do I create an if statment out of it? I wan't to use this to limit the abilitys of IE.
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 05 July 2006 - 10:07 PM

There is a get_browser() function in php

Just look it up in php.net

NoGray.com


#5 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 05 July 2006 - 10:26 PM

I just keep getting error messages with the get_browser() function! I tried the examples there at php.net.
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#6 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 06 July 2006 - 12:20 AM

I am not sure if you notice this part

Note: In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.

browscap.ini is not bundled with PHP, but you may find an up-to-date php_browscap.ini file here.



if the get_browser() doesn't work on your server, you can use an alternative at http://alexandre.ala...local-browscap/

NoGray.com


#7 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 06 July 2006 - 02:10 AM

lol, yes, I did miss that. I went through it again 3 other times before I seen it. Anyways, it seems to me as though it is the same as $_SERVER[HTTP_USER_AGENT]. Is it not?
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#8 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 06 July 2006 - 02:44 AM

FYI:

http://techpatterns....on.php#php_full

http://www.apptools....ptools/browser/

http://www.sitepoint...suck-php-rescue

http://www.phpclasse...ckage/2669.html


#9 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 06 July 2006 - 06:05 PM

it seems to me as though it is the same as $_SERVER[HTTP_USER_AGENT]. Is it not?


It uses the $_SERVER[HTTP_USER_AGENT] to get the browser info, but it also uses a database to fill in all the details, Here is a sample output

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [css] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [backgroundsounds] =>
    [vbscript] =>
    [javascript] => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>
)


as you can see, you'll get all the browser details such as the name, version, support for css, frames, javascript, java, etc etc.... (the support part is if the browser is capable, and not if the user have it turned on or off).

NoGray.com


#10 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 09 July 2006 - 01:47 AM

Yeah, but that is over kill and unnecessary. All I require is the browser type (Firefox) and the browser version (1.5.0.4).
I now know how to get it ($_SERVER[HTTP_USER_AGENT]). This is what I get in my FF, “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4.” How do I only pull out parts of this code?

Insight:
I need this to determine the level of accesses the user is given based upon the browsers capability to display semi-transparent ping images.

More insight:
I am in the process of re-scripting my site with the feature of themes. This way the user has the ability to choose a theme and with that theme there will be new colors, new background, and new photos. Unfortunately, not all browsers have the ability to support the semi-transparency of ping images. This is not my concern, my concern is how to nitpick the response from the $_SERVER[] code.
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#11 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 09 July 2006 - 05:45 AM

$browser = get_browser(null, true);
$browser = $browser['parent'];

This would get the browser name and version (i.e. Firefox 1.5).

#12 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 09 July 2006 - 06:16 PM

In my php.ini file (located at C:\wamp\Apache2\bin),  this is the only thing about the browscap.ini
[browscap]
;browscap = extra/browscap.ini

I put my browscap.ini file in C:\wamp\Apache2\bin\extra
and used this script:
<?php $_SERVER['HTTP_USER_AGENT']; $browser = get_browser(null, true); $browser = $browser['parent']; echo $browser; ?>

this is the error message I always get:
Warning: get_browser() [function.get-browser]: browscap ini directive not set. in C:\wamp\www\Tests\alignment.php on line 1

What am I doing wrong? Why isnt it working?
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 July 2006 - 06:18 PM

Did you restart the Apache webserver after you modified the php.ini file?

Ken

#14 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 09 July 2006 - 06:24 PM

Actually I didnt modify the php.ini file. It already had:
[browscap]
;browscap = extra/browscap.ini

I just created the folder "extra" and placed my browscap.ini file in it.
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#15 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 July 2006 - 06:25 PM

You need to remove the initial semi-colon from the beginning of that line, since that makes it into a comment. Save the file and restart Apache.

Ken

#16 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 09 July 2006 - 06:44 PM

And after doing that you can delete the browscap.ini file in your directory (it's not being used).

#17 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 09 July 2006 - 07:42 PM

Thanks, I no longer have the error message. However, I don't have any message.

This is the browscap file that I am useing (it was to big to post it):
http://browsers.gary...PHP_BrowsCapINI

It says it is a special browscap file and maybe thats why it doesnt show anything when I use:
<?php $_SERVER['HTTP_USER_AGENT']; $browser = get_browser(null, true); $browser = $browser['parent']; echo $browser; ?>

So how do I make this work? Maybe I just need a normal browscap.ini file but I cant find one...
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#18 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 10 July 2006 - 02:31 PM

Bumpady Upady
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#19 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 July 2006 - 02:43 PM

1. Did you rename it from php_browscap.ini to browscap.ini?
2. Where did you put it?

#20 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 10 July 2006 - 11:11 PM

1. Did you rename it from php_browscap.ini to browscap.ini?
2. Where did you put it?

1.)Yes, I have renamed the file to browscap.ini
2.) I placed it in, "C:\wamp\Apache2\bin\extra". I have placed it in this file because the php.ini file points to, "[browscap] browscap = extra/browscap.ini"
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users