防止PHP泄漏变量,包括或需要

发布于 2025-01-25 11:05:56 字数 894 浏览 4 评论 0原文

我有不需要的参数泄漏到我的template.php中。

function template($template, $args) {
  $foo = $args['bar'];

  extract($args);

  include __DIR__ . '/' . $template . '.php';
}

template.php

  • $ foo应该不打印出某些内容,因为它不是从$ $ args中提取的。
  • $ bar应该打印出一些东西,因为它是从$ args中提取的。确实如此。
  • $ Template应该打印出某物,因为它不是从$ args中提取的。
<!DOCTYPE html>
<?= $foo; ?>
<?= $bar; ?>
<?= $template; ?>
</html>

在现实生活中,上述HTML是有效的。另外,我在模板函数中还有更多参数。

我该如何解决论点泄漏?我可以通过在另一个功能中包装包装来更改范围,但是我仍然需要$ template,然后将其传递给template.php

有什么想法吗?

I have unwanted arguments leaking into my template.php.

function template($template, $args) {
  $foo = $args['bar'];

  extract($args);

  include __DIR__ . '/' . $template . '.php';
}

In the template.php

  • $foo should not print out something because it's not extracted from $args.
  • $bar should print out something because it's extracted from $args. It does.
  • $template should not print out something because it's not extracted from $args.
<!DOCTYPE html>
<?= $foo; ?>
<?= $bar; ?>
<?= $template; ?>
</html>

In real life the HTML above is valid. Also, I have much more arguments in the template function.

How can I get around argument leaking? I could change scope by wrapping include in another function but then I still need $template which will then be passed to the template.php.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文