Process mathematical equations in php

后端 未结 8 1923
陌清茗
陌清茗 2020-11-30 14:14

A user is allowed to enter any mathematical equation they like (with one variable):

x + 5

1 - x/2

(x/3) * (56/13)

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 14:35

    Eval is not Evil!!!!!

    Yes it can stuff your system up completely if you write bad code - but recent PHP versions can parse an invalid expression without crashing the whole script. And there are many other ways of exposing your system by writing bad code.

    That just leaves the possiblity of code injection attacks - which can easily be avoided by doing a preg_replace on everythnig which is not a safe character (i.e. 0....9, (, ), +, -, *, /, ^, .)

提交回复
热议问题