3

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

Blender
  • 289,723
  • 53
  • 439
  • 496
Ben Carey
  • 16,540
  • 19
  • 87
  • 169
  • I would go easy with the bold text. Adding too much to your question makes it hard to read. – Blender Aug 22 '12 at 06:50
  • how do you update the browsecap.ini ? – Dr.Molle Aug 22 '12 at 06:53
  • @Blender Noted, sorry about that :-) – Ben Carey Aug 22 '12 at 06:53
  • @Dr.Molle basic scheduled task that runs every morning. It downloads the browscap file from Gary Keiths website and then overwrites the current one on my server – Ben Carey Aug 22 '12 at 06:54
  • Did you make sure you use the "PHP" version from http://tempdownloads.browserscap.com/? My page took 1.5+ seconds to load, but when I replaced my browscap.ini with the Lite/PHP one, it went down to just a few hundreds of ms. I think the syntax in the ASP ones can cause performance issues when used in PHP. – tomconte Aug 07 '13 at 14:25

4 Answers4

6

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:

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
ThaDafinser
  • 499
  • 3
  • 13
0

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.

  • This is not up to date, and no longer exists. The download link is broken :-( – Ben Carey Aug 22 '12 at 07:24
  • I've updated my answer to include my version so you can download it. –  Aug 22 '12 at 09:33
  • This is a good class, and I appreciate your help, but as this is not maintained, it is not reliable enough for me to use. I have had to temporarily resort to using a workaround. Will post up in a min. +1 for your help – Ben Carey Aug 22 '12 at 11:02
0

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

Ben Carey
  • 16,540
  • 19
  • 87
  • 169
0

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.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
  • 1
    I'd love to take a look at your scripts. Do you still have them? – Erick May 06 '15 at 05:12
  • yup, I'll upload 'em for you this evening – m02ph3u5 May 06 '15 at 15:53
  • Thanks. I looked at parsing the browscap.ini, but it's a mess and even using the csv version - fgetcsv fails miserably on it. – Erick May 06 '15 at 17:19
  • You'll find them here: http://www.black-snow.net/tmp/ Keep in mind that they were never ment to be used in the "outside world". For good results you may have to query your db for the "parent" elements, too and merge the stuff together. Also keep in mind that I've written thos in 2k14 - I don't know if they work w/ current browscaps. – m02ph3u5 May 07 '15 at 17:26
  • Crap. I just got back to them and they're no longer there. Can we try again? Thanks! Maybe throw them on pastebin or somewhere? – Erick May 11 '15 at 04:18
  • They are still there. I'll turn this into a git repo this weekend. – m02ph3u5 May 12 '15 at 10:34
  • At the recommendation of a colleague, I tried piwik device detector. Whoa. Very easy and insanely fast. Best solution I've found. It's a regex-based deal. You might want to consider it. Here are the instructions I wrote for making PDD work without the rest of Piwik. http://pastebin.com/SXBYaC6B – Erick May 13 '15 at 06:06
  • Yup. It's awesome. Love it. Fast, reliable, low overhead. – Erick May 15 '15 at 04:44
  • http://stackoverflow.com/questions/30011230/how-to-use-piwik-device-detector-in-php-project/ – Erick May 15 '15 at 04:55