Jump to content

Archived

This topic is now archived and is closed to further replies.

bobleny

Detecting a users browser with PHP?

Recommended Posts

How can I detect a users browser type and version? (e.g. Internet Explorer 6.2)

Share this post


Link to post
Share on other sites
All info like that are stored in the $_SERVER variable.

$_SERVER[HTTP_USER_AGENT] == users browser

Hope that helped ;D

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
There is a [url=http://us2.php.net/manual/en/function.get-browser.php]get_browser()[/url] function in php

Just look it up in php.net

Share this post


Link to post
Share on other sites
I just keep getting error messages with the get_browser() function! I tried the examples there at php.net.

Share this post


Link to post
Share on other sites
I am not sure if you notice this part
[quote]
Note: In order for this to work, your [url=http://us2.php.net/manual/en/ref.misc.php#ini.browscap]browscap[/url] 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 [url=http://browsers.garykeith.com/downloads.asp]up-to-date php_browscap.ini[/url] file here.
[/quote]


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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
FYI:

http://techpatterns.com/downloads/php_browser_detection.php#php_full

http://www.apptools.com/phptools/browser/

http://www.sitepoint.com/article/browsers-suck-php-rescue

http://www.phpclasses.org/browse/package/2669.html

Share this post


Link to post
Share on other sites
[quote]
it seems to me as though it is the same as $_SERVER[HTTP_USER_AGENT]. Is it not?
[/quote]

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

[quote]
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] =>
)

[/quote]

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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]$browser = get_browser(null, true);
$browser = $browser['parent'];[/code]

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Did you restart the Apache webserver after you modified the php.ini file?

Ken

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
And after doing that you can delete the browscap.ini file in your directory (it's not being used).

Share this post


Link to post
Share on other sites
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.garykeith.com/stream.asp?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...

Share this post


Link to post
Share on other sites
1. Did you rename it from php_browscap.ini to browscap.ini?
2. Where did you put it?

Share this post


Link to post
Share on other sites
[quote author=Daniel0 link=topic=99583.msg394449#msg394449 date=1152542617]
1. Did you rename it from php_browscap.ini to browscap.ini?
2. Where did you put it?
[/quote]
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"

Share this post


Link to post
Share on other sites
I found another browscap.ini file. In the process of changing the files, I decided to try the code with out the browscap file and my results where the same, no errors or words. So something’s wrong here, and it’s probably my fault. Can’t any one help?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.