包含来自 PHP 流的代码

发布于 2024-10-06 12:04:39 字数 258 浏览 2 评论 0原文

我想知道是否有可能创建一个流包装器,以便从数组中加载一些代码,并使用类似下面的内容

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独﹏钓一江月 2024-10-13 12:04:39

我知道这是一个老问题......但我认为值得注意的是,你可以这样做:

$content = '
  <?php if($var=true): ?>
    print this html
  <?php endif; ?>
';

通常这对评估来说非常麻烦,但你可以这样做:

include "data://text/plain;base64,".base64_encode($content);

它会像这样快速解析它!

I know this is an old question...but I think it's worth noting that you can do something like:

$content = '
  <?php if($var=true): ?>
    print this html
  <?php endif; ?>
';

Normally this would very cumbersome to eval, but you can do:

include "data://text/plain;base64,".base64_encode($content);

And it will parse it snappy like!

爱殇璃 2024-10-13 12:04:39

Include 可以采用任意 url。阅读。下面是从那里获取的示例 HTTP 代码:

<?php

/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

?>

只需将其更改为 include "template://$thevalue";

Include can take arbitrary urls. Read this. Here is an example HTTP code taken from there:

<?php

/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

?>

And just change it to include "template://$thevalue";

Smile简单爱 2024-10-13 12:04:39

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.

假面具 2024-10-13 12:04:39

由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文