eval() 和 PHP 错误
我有一个像这样的 eval 函数
if(FALSE === @eval($code)) echo 'your code has php errors';
,所以如果代码有合成错误,它将返回该消息。
问题是,如果在代码中您有类似的内容:
require_once('missing_file.php');
它只会破坏页面,而没有我好的错误消息:(
对此有任何解决方法吗?
I have a eval function like this
if(FALSE === @eval($code)) echo 'your code has php errors';
So if the code has synthax errors it will return that message.
The problem is that if within the code you have something like:
require_once('missing_file.php');
it will just break the page, without my nice error message :(
Is there any workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,首先我希望
$code
来自可信来源,并且您正在执行用户发送的任意代码。其次,我认为解决方法的唯一方法是将 $code 保存到文件中,使用命令行 PHP 解释器运行它,然后检查退出值。请注意,通过此测试并不意味着
$code
不会出现致命错误,只是碰巧该脚本的特定执行没有引发任何致命错误;可能还有其他代码路径触发此类错误。这是因为一旦
eval
触发致命错误,就无法恢复,脚本就会终止。如果出现解析错误,eval
仅返回FALSE
。Well, first I hope that
$code
comes from a trusted source and that you're executing arbitrary code sent by the users.Second, the only way I see you can workaround that is to save
$code
into a file, run it with the command line PHP interpreter, and check the exit value. Note that passing this test doesn't make$code
fatal error free, it just so happened that this particular execution of the script did not throw any fatal error; there may be other code paths that trigger such an error.This is because once
eval
triggers a fatal error, it can't be recovered and the script dies.eval
only returnsFALSE
if there is a parsing error.