Alternative to system ("php -l")?

In the project I'm working on now, we are considering the possibility of placing system() in php. ini disable_functions . Now, one metamodem that ultimately will also fall prey to this restriction is syntax checking files with system("php -l"); calls system("php -l"); - prompting me to look for alternatives.

php_check_syntax() there, but not only this is not limited to just checking the syntax and including the file if it was syntactically valid, but was deleted from PHP 5.0.5. The manual suggests php -l instead, but provided that I am sure that disabling system call functions in PHP is a fairly common practice, I wonder if there is an accepted β€œbest” way to check the syntax of PHP files from within PHP files.

(By the way, I am not inclined to this, but β€œno” is enough (and I hope that this is indeed so). We can exclude the module from this restriction - but I ask this question both out of curiosity and also in the hope of a more elegant solution .)

+4
source share
3 answers

I found an alternative using PECL runkit_lint_file () .

It performs the same check as php_check_syntax ().

I think it's worth a look.

+3
source

This may be an option: When (if ever) eval NOT evil?

And it seems faster:

 $nTestTiempo0 = microtime(true); exec('php -l yourfile.php',$arrMsgError,$nCodeError); $nTestTiempo1 = microtime(true); echo "\n", '<p>Time in verify file with exec : '.($nTestTiempo1-$nTestTiempo0).' secs.</p>'; //Time in verify file with exec : 0.033198118209839 secs. $nTestTiempo0 = microtime(true); ob_start (); var_dump(eval('return true; if(0){?>'.file_get_contents('yourfile.php').'<?php };')); $arrMsgError = explode("\n",trim(ob_get_contents())); ob_end_clean(); $nTestTiempo1 = microtime(true); echo "\n", '<p>Time in verify file with eval : '.($nTestTiempo1-$nTestTiempo0).' secs.</p>'; //Time in verify file with eval : 0.00030803680419922 secs. $nTestTiempo0 = microtime(true); @system('php -l yourfile.php',$nCodeError); $nTestTiempo1 = microtime(true); echo "\n", '<p>Time in verify file with system : '.($nTestTiempo1-$nTestTiempo0).' secs.</p>'; //Time in verify file with system : 0.032964944839478 secs. 
+2
source

See our PHP Formatter . This command line tool accepts a well-formed PHP file and formats it well.

It not only formats, but also performs syntax checks and returns a command line status indicating whether the file was "correct"; it contains the full PHP 5 parser. Since this is a command line tool, it would be easy to run with a PHP script if this is what you need to do, and by checking the return status, you will find out if the file was legal.

0
source

All Articles