i am trying to implement a function that you can pass a formula into as well as a array that'll become variables for the formula, but due to the way it works i had to use the eval function, which i know is insecure, so i tried looking into possible dangers and to somewhat protect against the most troublesome dangers i made sure it doesn't eval if input preg matches with exec or " , however is this secure enough or are there other dangers beside exec that i should eliminate? here my code:
<?php
function calc($formula,$variables){
extract($variables);
if(!preg_match('/"/',$formula) && !preg_match('/exec/',$formula)){
eval("\$formula=$formula;");
echo $formula;
}
}
calc('0.025*$r-($r-100000)*0.01',@array(r=>1000000));
?>
update: thanks for not downvoting so far, i know this might not be the right place to ask, or not the proper way, but i was kinda struggling on how to ask this anyway
at this moment i discover by your comments eval really isn't safe even with these small "protections" however, i am wondering now on how to else make a function for people to input a formula and variables that might be part of it in order to get the evaluation, so any good tips on that?