Testing files for inclusion
Moderator: General Moderators
Testing files for inclusion
I need a method/function/snippet that will allow me to test a file for inclusion.
I deeply prefer not to use eval (and the php -l method). I've tried include, require, output buffering, file get contents..
Basically, I have a (php) file, that I am going to include, and that file might have a parse error. I need to be able to test for that parse error so that the script can keep going, and be able to report that it had an error.
Any ideas?
I deeply prefer not to use eval (and the php -l method). I've tried include, require, output buffering, file get contents..
Basically, I have a (php) file, that I am going to include, and that file might have a parse error. I need to be able to test for that parse error so that the script can keep going, and be able to report that it had an error.
Any ideas?
-
pentiumhead
- Forum Newbie
- Posts: 9
- Joined: Wed Aug 17, 2005 11:24 am
There is a feature in php in which an include file can return a value like this:
And the adder.php file will have:
Think on those lines i think it might work.
Kay
Code: Select all
<?php
$var1 =10;
$var 2= 20;
$var3 = include("adder.php");
?>Code: Select all
<?php
$var3 = $var1+$var2;
return $var3;
?>Kay
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I couldn't recall if you're running php5 or not, but.... php_check_syntax()
otherwise, I can only think up, after doing the lint run, hooking the error system.. but you'd still bomb on a Fatal...
otherwise, I can only think up, after doing the lint run, hooking the error system.. but you'd still bomb on a Fatal...
It doesn't for files that have a parse error. It simply dies at the first parse error.pentiumhead wrote:There is a feature in php in which an include file can return a value like this:
Think on those lines i think it might work.
Kay
Unfortunately, the game will be deployed to a wide variety of hosts, ranging from php4.22 up to php5.1+.feyd wrote:I couldn't recall if you're running php5 or not
Do you mean using php -l, for a lint run? Expand your meaning a little bit here, please.feyd wrote:after doing the lint run, hooking the error system..
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Yes, sorry, php -l is what I was referring to as a lint run. Another idea, a page subrequest? It'll run the code, but it will readily show you if there are errors in the script, and should generally be supported on most installs...Roja wrote:suckage.. hmmm...Roja wrote:Unfortunately, the game will be deployed to a wide variety of hosts, ranging from php4.22 up to php5.1+.feyd wrote:I couldn't recall if you're running php5 or not
Roja wrote:Do you mean using php -l, for a lint run? Expand your meaning a little bit here, please.feyd wrote:after doing the lint run, hooking the error system..
What do you mean by a page subrequest? Can you explain further?feyd wrote:Yes, sorry, php -l is what I was referring to as a lint run. Another idea, a page subrequest? It'll run the code, but it will readily show you if there are errors in the script, and should generally be supported on most installs...
I think it might mean something like:
That that still tries to run the code (albeit without the current script's context....
Code: Select all
$testPage = exec("wget $baseURL/$scriptToTest");
// now preg match for "error/warning/notice"
// if it passes then include it- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
absolutely I can further explain. Say you have a test_include.php that can be told some reference to a file on the server to test if it doesn't have any issues upon inclusion. You can have that script hook the error handlers so it can print more "structured" errors as well as making sure error_reporting and whatnot is at the levels you want to work with.
Your main script would generate a subrequest like so: file_get_contents('http://www.<site base url>.whatever/test_include.php?testMe.php') or whatever.. It'd add some time to processing, because php would be making URL requests to the server, but no actual errors could show up on this (the main script's) side.
The test_include.php script could do output buffering and capture the standard output of the script if it's supposed to output anything.
Your main script would generate a subrequest like so: file_get_contents('http://www.<site base url>.whatever/test_include.php?testMe.php') or whatever.. It'd add some time to processing, because php would be making URL requests to the server, but no actual errors could show up on this (the main script's) side.
The test_include.php script could do output buffering and capture the standard output of the script if it's supposed to output anything.
I tried doing an error handler, but I admit I didn't try file_get_contents.. its a file that changes status, so I wouldn't want it executed, but there might be some ways around that.feyd wrote:absolutely I can further explain. Say you have a test_include.php that can be told some reference to a file on the server to test if it doesn't have any issues upon inclusion. You can have that script hook the error handlers so it can print more "structured" errors as well as making sure error_reporting and whatnot is at the levels you want to work with.
Your main script would generate a subrequest like so: file_get_contents('http://www.<site base url>.whatever/test_include.php?testMe.php') or whatever.. It'd add some time to processing, because php would be making URL requests to the server, but no actual errors could show up on this (the main script's) side.
The test_include.php script could do output buffering and capture the standard output of the script if it's supposed to output anything.
I'll give fgc a try tonight. Thanks for the idea.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
cool, yeah the files would sort-of need to be in a format that supports running the "critical" bits without (or with little input) .. however, you could use cURL to do the request to send additional headers, say if it needs info about the user current being run under or something.. Granted, that will eat even more time in processing, but will make sure no errors so to a client 
Could be fairly uncommon, but might be a daily or weekly affair.. it all depends on how active things get in the community. Not sure.nielsene wrote:How often do these files get uploaded/changed?
Currently, they range from 6 lines per file to over 1500 lines in another. We are working to get the average down to around 200-300 lines.nielsene wrote:How complicated are they?
Actually, they already are.. thats why I need the parse test - to make it easy for an admin to see if it works or not.nielsene wrote:Would it be feasible to just put them into a "holding" cell until an admin can manually review/test in a sandbox?
Edit: Apparently it's pretty b0rken.. 
Anyway, http://pear.php.net/package/PHP_Parser might interest you
Anyway, http://pear.php.net/package/PHP_Parser might interest you
Like so many things in PEAR, can't use it..timvw wrote:Edit: Apparently it's pretty b0rken..
Anyway, http://pear.php.net/package/PHP_Parser might interest you
- License incompatible with GPL
- Not installed consistently on hosting providers
- Too difficult for non-admins to install properly
Whats "pretty b0rken"?