Eval: Error Handling Potentially fatal code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluedoor
Forum Newbie
Posts: 2
Joined: Thu Nov 02, 2006 8:59 am

Eval: Error Handling Potentially fatal code

Post by bluedoor »

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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I'm working on a CMS and, of course, am using eval to took process the code coming from the database.
"Of course"? It's not that common to use eval at all.
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.
You can output template via register_shutdown_function(). It will get processed even if there was a fatal error somewhere in the script.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

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.
interesting....i never knew it could kep going in a fatal error.............pretty cool!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

interesting....i never knew it could kep going in a fatal error.............pretty cool!
try this:

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
?>
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Personally I'd rather put a marker in the DB and the code in a function in a file. You could even include arguments in the DB but not the code.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

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.
bluedoor
Forum Newbie
Posts: 2
Joined: Thu Nov 02, 2006 8:59 am

Post by bluedoor »

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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

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.
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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Also why do you think evaling code would be slower than opening a file?
Stab in the dark: eval bypasses opcode caches.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Weirdan wrote:
Also why do you think evaling code would be slower than opening a file?
Stab in the dark: eval bypasses opcode caches.
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.

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);

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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.
What those things you mentioned have to do with opcode cache?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Weirdan wrote:What those things you mentioned have to do with opcode cache?
Pass! Is that a leading question?
Post Reply