Browscap.ini throwing an error when loading PHP (command line - PHP_CLI)
Asked Answered
O

3

11

I have a cronjob that summarize browser statistics. This cronjob loads data and then use the get_browser() PHP function to parse the browser information.

Here's what I did:

cd /etc/php5/cli/conf.d
me@ubutnu:/etc/php5/cli/conf.d$ sudo wget http://browsers.garykeith.com/stream.asp?Lite_PHP_BrowsCapINI -O browscap.ini
2011-09-30 15:14:18 (890 KB/s) - `browscap.ini' saved [185384/185384]

Then the cronjob run:

php /usr/local/cron/summarizeStats.php --option=browserStats --date=yesterday

and I get this error:

PHP:  syntax error, unexpected $end, expecting ']' in /etc/php5/cli/conf.d/browscap.ini on line 51

What am I doing wrong? Thanks

Overrun answered 30/9, 2011 at 19:42 Comment(1)
I just downloaded the latest browsecap file for php, and it is over 20,000 lines, so if it is saying unexpected $end at line 51, then the file you have is incomplete. Unexpected $end means that php encountered the end of a file when it wasn't expecting to. Did you download the browsecap file that is PHP compatible? Here is a link to the latest php browsecap.iniBedspread
C
28

There is seemingly right now an error with those browsecap files. They seem to contain unescaped semicolons ";" in the browser spec. You can fix that using this little script:

<?php
$browsecap = file('browscap.ini');
foreach( $browsecap as &$row )
    if ( $row[ 0 ] == '[' )
        $row = str_replace( ';', '\\;', $row );

file_put_contents( 'fixed_browscap.ini', $browsecap );
Comedietta answered 30/9, 2011 at 20:56 Comment(1)
Didn't work for me. IEs where not recognized. I replaced the semicolons and ticks with asterisks inside my shell script and it worked again. Example: sed -e '/^\[/ s/;/*/g' /etc/php5/apache2/browscap_orig.ini > /etc/php5/apache2/browscap.ini sed -i "/^\[/ s/'/*/g" /etc/php5/apache2/browscap.iniNovocaine
M
1

A little bit late, but there are still problems with using file without modifications. I'm using following script to download and change browscap.ini so it can work on my server.

#!/bin/sh
url="http://browscap.org/stream?q=PHP_BrowsCapINI"
curl -L -o browscap.ini ${url}
sed -I "" -E 's/;/\\;/g' browscap.ini
sed -I "" -E 's/[\\;]{40}/;;;/g' browscap.ini
sed -I "" -E "s/\'/\\\'/g" browscap.ini
mv browscap.ini /usr/local/etc/php/browscap.ini

Explanation

  • 1st sed is escaping every ; with \'
  • 2nd sed is returning comments to previous state (slow), replacing just 4 or 5 semicolons will cause error because there is some section with string like this (;;;;). This could be optimised with something like ^\; in search part, and just single ; in replace part, need to test that before I put
  • 3rd sed is escaping single quote used in "Let's encrypt..." sections and in couple other places like this '*'

Don't forget to adjust your browscap.ini finial destination. Also there is no need for Apache or PHP restart after update, so put this script somewhere and setup cron job.

Myramyrah answered 5/12, 2019 at 10:35 Comment(0)
C
0

sed can be used to escape the semi-colon like so:

sed 's/;/\\\;/g' browscap.ini > browscap_escape.ini

This will catch all the comments as well but you could use sed again to catch those.

As described here github.com/browscap/browscap/issues/119

Chokeberry answered 20/12, 2017 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.