Eval: Error Handling Potentially fatal code
Moderator: General Moderators
Eval: Error Handling Potentially fatal code
Hi all -
I'm working on a CMS and, of course, am using eval to took process the code coming from the database. I'm not too worried about this security wise, as only admins will have access to modifying the evaled code.
Anyways, what I want is that if there is some kind of error in the evaled code, the template will still show up, just that the content might be messed up or not there. Right now, when the content has a fatal error, the entire script halts executing, and thus the template never has a chance to output.
Is it possible to handle the errors in the evaled code? If there's a fatal error in the evaled code, I don't want it to stop everything!
Thanks,
Ryan
I'm working on a CMS and, of course, am using eval to took process the code coming from the database. I'm not too worried about this security wise, as only admins will have access to modifying the evaled code.
Anyways, what I want is that if there is some kind of error in the evaled code, the template will still show up, just that the content might be messed up or not there. Right now, when the content has a fatal error, the entire script halts executing, and thus the template never has a chance to output.
Is it possible to handle the errors in the evaled code? If there's a fatal error in the evaled code, I don't want it to stop everything!
Thanks,
Ryan
"Of course"? It's not that common to use eval at all.I'm working on a CMS and, of course, am using eval to took process the code coming from the database.
You can output template via register_shutdown_function(). It will get processed even if there was a fatal error somewhere in the script.Anyways, what I want is that if there is some kind of error in the evaled code, the template will still show up, just that the content might be messed up or not there. Right now, when the content has a fatal error, the entire script halts executing, and thus the template never has a chance to output.
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
interesting....i never knew it could kep going in a fatal error.............pretty cool!Weirdan wrote: You can output template via register_shutdown_function(). It will get processed even if there was a fatal error somewhere in the script.
try this:interesting....i never knew it could kep going in a fatal error.............pretty cool!
Code: Select all
<?php
function at_request_end() {
var_dump('AT END!');
}
register_shutdown_function('at_request_end');
asd(); // fatal error, call to undefined function
?>- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
You would be better off saving those code snippets for your database into files that will be loaded as needed. Storing code in a database and then using the eval is very, very bad practice as well as very, very SLOW.
A database should be used to store DATA and not program code. You will find including files is many times faster and uses far fewer CPU cycles than evaling code pulled from a database.
A database should be used to store DATA and not program code. You will find including files is many times faster and uses far fewer CPU cycles than evaling code pulled from a database.
Thanks Weirdan - that looks great, exactly what I was searching for but couldn't find.
With regard to putting code in a database, I was not aware that evaling code from the database was so much slower. I thought that might be the case, but didn't know.
Since I'm storing content in the db, there's inevitably going to be code stored there. However, as bokehman suggested, if I keep the code in my db down basically to 'include' statements, will this avoid the speed problem? This may be more of a database question, but I wanted to keep it where since we've started to discuss it.
With regard to putting code in a database, I was not aware that evaling code from the database was so much slower. I thought that might be the case, but didn't know.
Since I'm storing content in the db, there's inevitably going to be code stored there. However, as bokehman suggested, if I keep the code in my db down basically to 'include' statements, will this avoid the speed problem? This may be more of a database question, but I wanted to keep it where since we've started to discuss it.
Why? If you are already accessing the DB to get your content why would it be slower because that content contains some code. Also why do you think evaling code would be slower than opening a file? It is exactly what happens when a file is included.AKA Panama Jack wrote:You will find including files is many times faster and uses far fewer CPU cycles than evaling code pulled from a database.
If you are already pulling the content from the DB there would be no extra overhead if it contained code. Time saved ZERO. As far as the difference between eval and require eval is far quicker. The following code returned 440 microseconds for require and 29 microseconds for eval. The evaled code was the same as in the required file.Weirdan wrote:Stab in the dark: eval bypasses opcode caches.Also why do you think evaling code would be slower than opening a file?
Code: Select all
<?php
microtime(true);
$start = microtime(true);
require('test53.php');
$end = microtime(true);
echo (($end - $start)*1000000).'<br>';
$start = microtime(true);
eval('$array = array(1,2,3,4,5);');
$end = microtime(true);
echo (($end - $start)*1000000);
?>