php - 使用php读取php文件的源码

标签 php remote-file-inclusion

我正在尝试使用 php 读取单独的 php 文件的源代码。我尝试按以下方式使用 file_get_contents

file_get_contents('http://www.example.com/someFile.php');

不幸的是,上面的代码尝试执行 php 代码,而不是像读取任何其他文件那样仅仅读取文本。

我遇到了an article这似乎解决了这个问题,这让我得到了以下代码:

file_get_contents('php://filter/convert.base64-encode/resource=http://www.example.com/someFile.php');

但是这段代码具有相同的效果;它尝试执行 php 代码。

如何使用 php 读取另一个 php 文件的源代码,而不尝试执行文件中的 php?

最佳答案

Unfortunately, the code above attempts to execute the php code rather than just reading the text as it would with any other file.

不,事实并非如此。

这里发生的情况是,file_get_contentshttp://www.example.com/someFile.php 发出常规 HTTP 请求,并且远程 “example.com”上的服务器正在解释 PHP 代码。它提供的结果与您在浏览器中导航到 http://www.example.com/someFile.php 完全相同。您的脚本正在下载该输出。

file_get_contents 绝对不会在检索文件后执行该文件的内容。您的脚本对“someFile.php”的唯一访问是远程服务器愿意提供的; file_get_contents 无法以某种方式获取底层 PHP 源代码,就像您无法通过浏览器以某种方式查看 PHP 源代码一样。

关于php - 使用php读取php文件的源码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9508858/

相关文章:

php - 显示正确的记录

php - 在 Symfony 2 中用户切换(模拟)后重定向

php - 从 mysql/php 中的用户 ID 检索消息

php - Woocommerce pre_get_posts 导致问题

php - 分页:如何将长文本显示为书页?

linux - 通过中间机器挂载远程文件系统(sshfs)

jquery:有没有办法从主机目录读取所有文件?

PHP eval(gzinflate(base64_decode(..))) hack - 如何防止它再次发生?