0

I have downloaded the library browscap version 2.0 and added this code

$browscap = new Browscap('/tmp');
$browscap->doAutoUpdate = false;
$current_browser = $browscap->getBrowser();

The problem is when I am trying to get information about the current browser I'll have an error

Allowed memory size of 268435456 bytes exhausted (tried to allocate 281 bytes) in ....phpbrowscap-2.0/src/phpbrowscap/Browscap.php on line 677

I do not want to increase the memory because I will be losing performance since my code is executed on every load page. So I tried debugging the code, I noticed that he is checking a lot of browsers. There is a lot of values in $tmp_user_agents and he is looping over this variable.

Is there anyway to get the functionality of this library without taking too much time? Or am I missing something in how it works

beginner1
  • 65
  • 1
  • 8

2 Answers2

0

Well Browscap has a very very huge database and to load that (and iterate over it) on EVERY single page hit is of course not very efficient. The "ini" file I downloaded on their website has 7.5MB!!! That PHP runs out of memory is easy to understand. I would not recommend to use this library in this case.

I only checked their website and the resource file - not the code - but it seems as if they only look for the User-Agent String to verify the browser (which is very easy to fake by the way).

Have you ever looked at this PHP function: http://php.net/manual/de/function.get-browser.php Enough output for your needs?

Daniel K.
  • 1,189
  • 1
  • 10
  • 26
  • I just want to know if the browser is capable of accepting cookies but the problem with this function that I need to make alterations to php.ini which I don't have access to – beginner1 Jul 15 '14 at 16:08
  • You may try `ini_set()` or something like this: http://stackoverflow.com/questions/9448424/detect-if-cookies-are-enabled-in-php – Daniel K. Jul 15 '14 at 20:02
  • 'browscap' is changeable only in the system php.ini and/or httpd.conf. I cannot set it at the script level. As for you other suggestion, I wanted to avoid a page reload but I guess I have no other option. – beginner1 Jul 16 '14 at 07:20
  • You may use JavaScript to verify the capability. That wouldn't cause a reload and is executed by the client. But of course it's not very safe since people can disable JavaScript. But you may use JavaScript for verification and if this is NOT working you try the php reload-thing. So for most users your script will work and those "super-secure"-non-JavaScript-Freaks need to get along with the reload. – Daniel K. Jul 16 '14 at 07:27
0

I know this is a bit late to the party but the most efficient way to use it is to run a cron job on your server once a day to update the cache then in your code make it so it doenst update the cache when browscap is called.

In your site pages, run this code:

$browscap = new Browscap($cacheDir);
$browscap->doAutoUpdate = false;
$current_browser = $browscap->getBrowser();

On your server, create this script (i called it updateBrowscap.php), mine is in the same folder as browscap.php and the cache folder is a subfolder of it.

ini_set('memory_limit','1024M');    // allow 1GB for this script
set_time_limit(300);            // change to 5 minutes for this script

// CREATE THIS CRON SCRIPT TO RUN ONCE PER DAY
// /usr/bin/php -q [full-path-to-this-file]/updateBrowscap.php > [full-path-to-this-file]/backup.log 2>&1

// Loads the class
require 'Browscap.php';

// The Browscap class is in the phpbrowscap namespace, so import it
use phpbrowscap\Browscap;

echo 'started: '.date("d-m-Y H:i:s", time()).PHP_EOL;

$path = dirname(__FILE__).'/cache';

$bc = new Browscap($path);
$bc->updateCache();

echo 'finished: '.date("d-m-Y H:i:s", time()).PHP_EOL;

My script would not run using the maximum of 512MB that the shared host would let me allocate so I have manually set it in this script to 1024MB.

Once I resolved the memory issue I was hit with a timeout issue so I have increased that from 30 seconds to 5 minutes just for this script.

I run it once a day around 3am.

Greg J
  • 189
  • 2
  • 9