Automatically update your php_browscap.ini

19 10 2010

My website uses the PHP function get_browser to see if a client is Javascript enabled. (I use this primarily to see if the visitor is a search engine bot and should see a long list of data or a real person and can handle AJAX-loaded data in tabs – most real people these days are using Javascript-enabled browsers.)

get_browser relies on the php_browscap.ini file, and the problem is that keeping the php_browscap.ini file up-to-date can be a chore, and should certainly be done automatically. (I notice the problem now that I use Chrome, and with every major release, the browscap file has no clue about my new browser, which is silly.)

So I finally sat down and wrote a basic script to fetch the updated file and restart my web server, which I’ll run weekly. This works for my FreeBSD box – change the paths and download command to fit your installation.

/usr/local/bin/wget -Ophp_browscap.ini ""
mv -f /usr/local/etc/php/php_browscap.ini /usr/local/etc/php/php_browscap.old.ini
mv -f php_browscap.ini /usr/local/etc/php/
/usr/local/etc/rc.d/apache22 restart

I saved to: usr/local/www/
Then add it to your crontab:

# php_browscap update
5       4       *       *       6       root    /usr/local/www/ >> /var/log/cron.log

I run it Saturday morning.
Any questions?



One response

7 08 2012
Steven L.

Kinda took the update script to the next level. Probably went overboard but thought I would share. Also requires paths to be updated for your installation.

&1′, $download_filename, $browscap_url);
$apache_restart_cmd = ‘/usr/local/etc/rc.d/apache22 restart 2>&1’;

exec($wget_cmd, $wget_output);

if (file_exists($download_filename) && (filesize($download_filename) > 0)) {
$download_php_browscap = parse_ini_file($download_filename, true, INI_SCANNER_RAW);
$system_php_browscap = parse_ini_file($system_filename, true, INI_SCANNER_RAW);

if (isset($download_php_browscap[‘GJK_Browscap_Version’][‘Version’])) {
if ($download_php_browscap[‘GJK_Browscap_Version’][‘Version’] > $system_php_browscap[‘GJK_Browscap_Version’][‘Version’]) {
echo sprintf (“Updating version %d to %d\n”, $system_php_browscap[‘GJK_Browscap_Version’][‘Version’], $download_php_browscap[‘GJK_Browscap_Version’][‘Version’]);

if (copy($download_filename, $system_filename)) {
exec($apache_restart_cmd, $apache_output);
echo “Restarting apache:\n”;
echo implode($apache_output, “\n”);

if (!unlink($download_filename)) {
echo “Unable to delete the downloaded browscap ini\n”;
} else {
echo “Unable to copy the downloaded browscap ini file into the system directory\n”;
} else {
$update_date = strtotime($system_php_browscap[‘GJK_Browscap_Version’][‘Released’]);
$days = (time() – $update_date) / 86400;
echo sprintf(“Browscap not updated in %d days\n”, $days);
} else {
echo “Download was not a recognized browscap ini file:\n”;
echo implode($wget_output, “\n”);
} else {
echo “Unable to download new php browscap ini file:\n”;
echo implode($wget_output, “\n”);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: