get_browser slowing down page load, any alternative?

5k views Asked by At

I have used PHP's get_browser function for quite some time now and have never really noticed any lag on any of my websites. However, recently I noticed that one of my sites was taking a second or so more to load at the server end than it should take. I commented out the get_browser function and the page loaded instantly.

Could this be to do with my server or is the get_browser function known to be slow? The website is the only website I have that is running on a windows server, could this be the issue? Or could this be to do with my browscap.ini file (this is updated everyday from http://browsers.garykeith.com)?

If there is nothing I can do to speed up the get_browser function, are there any alternatives to it? I need to reliably collect the following information about the browser:

  1. What browser it is, i.e. Chrome, IE, Safari etc
  2. What version it is (in full), i.e 10.1 etc
  3. Whether it is a crawler or a bot

I am not aware of any other methods of gathering this information from the user agent, would it be better to use javascript (I would rather not as I need the values server side)?

4

There are 4 answers

0
ThaDafinser On BEST ANSWER

I also experiences about a 5s delay when using get_browser so i also looked for another solution.

What works great and will surely be up to date for a long time is a lib in piwik:

1
Ben Carey On

So far I have not managed to find a suitable alternative to the get_browser function, nor have I been able to find why it is slow on my Windows server and not on my Linux.

For the moment I will apply a basic cookie workaround. On the first visit to my site, I will store the browser details in a cookie, and then retrieve these from then onwards. This is faster then using the get_browser function for the moment.

If anybody else has any other solutions, please comment or post them up

3
AudioBubble On

This class will come in handy: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/. I use it in many of my projects and it doesn't use get_browser, instead it looks at the useragent of the browser. I have updated my version with Windows Phone browser, you can download it at: http://cl.ly/code/1V3E1k1G3B25.

9
m02ph3u5 On

At present the full browscap.ini for PHP is about 10MB in size. A single call to get_browser() or other methods accessing that "database" requires a ridiculously high amount of resources.

That's why I changed the format and imported the data into a MySQL database. I wrote two simple scripts that allow me to easily update the database whenever a new browscap.ini is out. The first script generates the table structure from the browscap.ini file - the second script seeds the table. If you're interested in the scripts please tell me in the comments below and I'll upload them for you. They are full of spaghetti but they work.

Since browsers don't tend to change during a session you should fetch the data from browscap.ini only once per session and then store it.