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 mented 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 )?
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:
- What browser it is, i.e. Chrome, IE, Safari etc
- What version it is (in full), i.e 10.1 etc
- 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)?
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 mented 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.)?
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:
- What browser it is, i.e. Chrome, IE, Safari etc
- What version it is (in full), i.e 10.1 etc
- 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)?
Share Improve this question edited Aug 22, 2012 at 6:49 Blender 299k55 gold badges458 silver badges510 bronze badges asked Aug 22, 2012 at 6:46 Ben CareyBen Carey 17k20 gold badges93 silver badges180 bronze badges 5- I would go easy with the bold text. Adding too much to your question makes it hard to read. – Blender Commented Aug 22, 2012 at 6:50
- how do you update the browsecap.ini ? – Dr.Molle Commented Aug 22, 2012 at 6:53
- @Blender Noted, sorry about that :-) – Ben Carey Commented Aug 22, 2012 at 6: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 Commented Aug 22, 2012 at 6:54
- Did you make sure you use the "PHP" version from tempdownloads.browserscap.? 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 Commented Aug 7, 2013 at 14:25
4 Answers
Reset to default 6I 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:
- Piwik Device Detector
This class will e in handy: http://chrisschuld./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.
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 ment or post them up
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 ments 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.