executing code from database

前端 未结 8 462
逝去的感伤
逝去的感伤 2021-01-29 14:42

I have a PHP code stored in the database, I need to execute it when retrieved.

But my code is a mix of HTML and PHP, mainly used in echo \"\";

A sample that look

相关标签:
8条回答
  • 2021-01-29 15:27

    You can execute code with eval():

    $code_str = "echo 'Im executed'";
    eval($code_str );
    

    BUT PAY ATTENTION that this is not safe: if someone will get access on your database he will be able to execute any code on your server

    0 讨论(0)
  • 2021-01-29 15:31

    See eval. It lets you pass a string containing PHP and run it as if you'd written it directly into your file.

    It's not a common practice to store executable PHP in a database; is the code you store really that different that it makes more sense to maintain many copies of it rather than adapting it to do the same thing to static data in the database? The use of eval is often considered bad practice as it can lead to problems with maintenance, if there's a way of avoiding it, it's normally worth it.

    0 讨论(0)
提交回复
热议问题