php - Solution for eval -


i doing custom search table. have 3 search parameters: from, , status. have used eval() filter result according received parameter. below code:

$search = ($from != "" || $to != "" || $status != "" );    if ($search) {     if ($from != '') {         $condition[] = '$from ==  $res["from_number"]';     }     if ($to != '') {         $condition[] = '$to ==  $res["to_number"]';     }      if ($status != '') {         $condition[] = '$status == $log["status"]';     }      $search = "if(" . implode(' && ', $condition) . '){ return false; } else { return true; }'; } 

after getting conditions using eval

if (eval($search)) {  } 

my problem don't want use eval(). may cause security issues. ladder if else not possible, lengthy. other solution?

e.g. if have passed value status want check

if($status == $log["status"]) { } 

if have passed & number should like:

if($from ==  $res["from_number"] && $to ==  $res["to_number"]) { } 

don't use eval - potentially dangerous , not recommended use.

your code can this:

$result = false; if ($from != "" || $to != "" || $status != "") {     if ($from != '' && $from != $res["from_number"]) $result = true;     if ($to != '' && $to != $res["to_number"]) $result = true;     if ($status != '' && $status != $log["status"]) $result = true; }             if ($result) {     // ........ } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -