如何在数据库中保存php代码 [英] how to save php code in database

查看:125
本文介绍了如何在数据库中保存php代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在一个项目中,我必须保存一些PHP代码在数据库中,我保存代码与 PHP标签



但我没有得到如何检索的代码完全一样存储到数据库。



我尝试过以下方法:



a)直接检索:当我直接检索数据库$ row ['code']的值为空白



b)使用eval():当我使用eval()然后我得到语法错误: p>

语法错误,H:\ xampp\htdocs\trycodeonline\testing.php(27)中意外的'< )'d code on line 1



c)使用eval()没有php标签:当我删除php代码,结果而不是代码,像我要打印 echohello,但它正在打印hello



如何打印php代码?

解决方案

如果您真的想这样做,可以使用 eval()作为您的方法c)建议:

  $ var //您的变量与DB中的数据
<?php echo eval(?>。$ var。< ;?)?

有许多合法的使用情况,你想在数据库中存储PHP命令,我个人觉得大喊不要做,而不知道情况有点无益。



无论如何,正如在各种线程中已经指出的,有安全问题和其他陷阱使用这种方法,我会建议反对它。它会使调试更困难,并增加潜在的安全漏洞,等等...



阅读 this post and 的帖子,以了解更多关于使用eval()的陷阱。


Currently I am working on a project in which I have to save some PHP code in database, I am saving the code with PHP tags.

But I am not getting how I can retrieve the code exactly as stored into database.

I have tried the following methods :

a) Direct retrieval : When I am retrieving the databse directly then the value of $row['code'] is blank

b) using eval() : When I am using eval() then I am getting syntax error :

syntax error, unexpected '<' in H:\xampp\htdocs\trycodeonline\testing.php(27) : eval()'d code on line 1

c) using eval() without php tags : when I am removing the php codes then I am getting result instead of code, like I want to print echo "hello" but it is printing "hello"

So how can I print the php code ?

解决方案

If you really want to do it, you can use eval() as your method c) suggests:

$var // your variable with the data from the DB
<?php echo eval("?>". $var ."<?") ?>

There are many legitimate use-cases where you'll want to store PHP commands in a database, and I personally find shouting "dont do it" without knowing the circumstances a bit unhelpful.

Anyway, as has already been pointed out in various threads, there are security issues and other pitfalls using this approach and I would recommend against it. It will make debugging harder, and increases potential security vulnerabilities, and so on...

Read this post and this post to know more about the pitfalls of using eval().

这篇关于如何在数据库中保存php代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆