GaretJax / phpbrowscap

Standalone replacement for php's native get_browser() function
MIT License
429 stars 69 forks source link

Memory exhausted #68

Open theblackwidower opened 10 years ago

theblackwidower commented 10 years ago

So, I just decided to test out this little toy on my personal server, and I ran into a bit of an issue. This is the message I got:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /var/www/html/modules/Browscap.php on line 818

What exactly are you trying to do!? You may ask. Well, nothing more nor less than what was shown in the quick start guide.

On a likely related note, the cache folder. I'm not sure what it's supposed to do, but I created it, and directed the script to it, and unlocked it for full access (777), and yet nothing is filling it. I run the script, but I get the above error, and the folder remains empty.

Am I doing something wrong, or is this an error in the class?

Thank you for your time.

sidwubf commented 9 years ago

I got the same problem. I think it just change the "browscap.ini" to array. And the memory exhausted. Hope fix it.

ghost commented 9 years ago

I have the issue too, looking into it now...

ghost commented 9 years ago

Well, I had a plaster/bandaid fix for line 818, but that only shifted another memory issue to line 504! The fix was:

// Get updated .ini file
$browscap = $this->_getRemoteData($url);

// Chunk FIX
$bc_chunks = [];
$browscap = str_split($browscap, 25000);
foreach ($browscap as $chunk) $bc_chunks = array_merge($bc_chunks, explode("\n", $chunk));
$browscap = $bc_chunks;
// Chunk FIX

// $browscap = explode("\n", $browscap); --- removed for FIX

Alternatively, bump up your memory if you can (need access to php.ini)... 512MB worked.

To summarise, the INI file is HUGE

ghost commented 9 years ago

Guys, stop it right there! More memory won't do and trying to download on live system is not recommended because the file is way to large and will never stop to get large. Best to follow the original developer advise: cron the update and all should be ok. Cache folder needs 774 and chown to root:www-data ( Php identity ). We forked this pack and tweaked the construction method for more verification of the cache folder and set autoupdate to false. No more memory problem. Please see jean-lelarge/phpbrowscap jean.lelarge@ppkl.net

zergtant commented 9 years ago

ini_set('memory_limit', '-1'); overrides the default PHP memory limit. and before you run frist time use $bc->updateCache() to make cache. then you can remove ini_set('memory_limit', '-1'); this is my fix.

DaAwesomeP commented 9 years ago

Expanding the available memory is very dangerous. PHP could suck up memory normally used by other processes or simply grow uncontrollably until the system crashes. Also, setting the INI means that your instance of PHP will have full access to it, which is a security risk. Lastly, setting the INI affects all PHP processes and scripts, not just the one that browscap is being used in.

On Nov 9, 2014, at 9:15 AM, "hengtao tantai" notifications@github.com wrote:

ini_set('memory_limit', '-1'); overrides the default PHP memory limit. and before you run frist time use $bc->updateCache() to make cache. then you can remove ini_set('memory_limit', '-1'); this is my fix.

— Reply to this email directly or view it on GitHub.

DaAwesomeP commented 9 years ago

Another idea is to use another CLI program like curl or wget to download the browscap file. It should have no problem with the file.

The chunk loading is also great. Either way, the downloading should be done through cron. If the download crashes, then it won't affect the client loading a page. Also, the client's page loading won't be slowed due to browscap downloading. I have experienced that multiple times.

brendo commented 9 years ago

Currently using the workaround described by @zergtant to get this going. I feel this should be documented somewhere more officially though. Better still, it'd be a neat to include a script that could be used via cron to get/update this file :+1:

mimmi20 commented 9 years ago

If you the library from browscap/browscap-php and create the issues there, they may be fixt earlier.

brendo commented 9 years ago

Looks like the issues have been fixed in another port

hamburger123456 commented 8 years ago

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 131072 bytes) in /www/XYZ/scripts/Browscap.php on line 833

500MB not enought? Whats to do? My cronJob: use phpbrowscap\Browscap; ini_set('memory_limit', '512M'); require 'scripts/Browscap.php'; $browscap = new Browscap('/www/XYZ/cache'); $browscap->updateCache();

mzvarik commented 5 years ago

I have the same problem... doesn't work.