View Full Version : parsing PHP string for browser detection

03-22-2004, 02:36 PM
I am writing a script to apply a different stylesheet to a page depending on whether the user is using IE or Mozilla.

1st part is fairly simple - browser is detected from the environment variable HTTP_USER_AGENT. This returns (for example):

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

I want to be able to split up this string so that only the browser type variable (i.e. the MSIE 6.0 in this case) is left. From that I can re-direct as appropriate

Any suggestions?

03-22-2004, 03:55 PM
Whenever you need something, and you think that there must have been lotts of other people that ever needed this, then a google will always turn up some ready made code. Like


but i'm sure there will be many other here http://www.google.be/search?q=php+browser+type&ie=UTF-8&oe=UTF-8&hl=nl&meta=

and probably in the PHP usermanual http://be.php.net/function.get-browser

03-22-2004, 04:20 PM
Remember that if you do server-side browser detection you need to set Private caching so that proxy-servers don't cache it. And also that server-side detection is unreliable, because browsers can mis-identify themselves.