包含来自 PHP 流的代码
我想知道是否有可能创建一个流包装器,以便从数组中加载一些代码,并使用类似下面的内容
<?php include 'template://myarraykey/'; ?>
,并使其像从文件中执行正常包含一样工作?询问的原因是因为我真的不想在文件系统上存储模板,它们要么存在于内存缓存中,要么存在于数据库表中,并且绝对不想使用 eval()。
另外我假设我需要将allow_url_include 设置为on?
I'm wondering if it is at all possible to create a stream wrapper in order to load some code from an array into using something like the following
<?php include 'template://myarraykey/'; ?>
and have it work like doing a normal include from a file? The reason for asking is because I don't really want to store templates on the file system, they'll either exist in memcache or a database table and defiantly don't want to use eval().
Also I would assume I'll need to have allow_url_include set to on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是一个老问题......但我认为值得注意的是,你可以这样做:
通常这对评估来说非常麻烦,但你可以这样做:
它会像这样快速解析它!
I know this is an old question...but I think it's worth noting that you can do something like:
Normally this would very cumbersome to eval, but you can do:
And it will parse it snappy like!
Include 可以采用任意 url。阅读此。下面是从那里获取的示例 HTTP 代码:
只需将其更改为
include "template://$thevalue";
Include can take arbitrary urls. Read this. Here is an example HTTP code taken from there:
And just change it to
include "template://$thevalue";
eval 这个词并不邪恶。你可以用它做的事情是。任何做你想做的事情的方法都会有与 eval 相同的风险。因此,只需使用 eval,因为保护它是一个更“已知”的问题。
eval, the word, isn't evil. Things you can do with it are. Any means of doing what you want will have the same risk as eval. So just use eval, since securing it is a more 'known' problem.
由于
include
可以使用任何适当的流,并且您可以注册自己的流包装器,所以我不明白为什么不这样做。只是为了好玩,您可以尝试另一种选择:从 memcached 加载数据并使用 包含它数据流包装器。
Since
include
can use any appropriate stream and you can register your own stream wrapper, I don't see why not.Just for fun you could try an alternative: load your data from memcached and include it using the data stream wrapper.