【问题标题】:PHP4 problems with include() within a file created by fwrite()由 fwrite() 创建的文件中包含 () 的 PHP4 问题
【发布时间】:2010-12-18 22:47:10
【问题描述】:

我有一个名为generator.php 的文件,它使用fwrite() 在服务器(Apache、PHP4)上创建result.php

result.php 中的一行是 PHP include() 语句。

所以,在generator.php

if (!is_file($fname)){
    $resultfile = fopen($current_path . "/" . $fname, "w+");
}
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "'. '/inc/footer.php"); ?>' . "\n");
fclose($resultfile);
chmod($current_path . "/" . $fname, 0755);  

result.php:

<h2>Sponsored Links</h2>
<!-- begin sidebar_top ad -->
<?php echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
  include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php"); ?>
<!-- end sidebar_top ad -->

但是当我在浏览器中访问result.php 时,include() 语句不起作用。 echo 语句可以,所以我知道路径是正确的。

另一个 test.php 使用相同的代码,我使用 FTP 将它上传到同一个文件夹,工作正常。

通过 FTP 恢复时,两个文件中的代码相同。

test.php:(工作、回显和包含正确。)

<?php 
echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php"); 
?> 

知道为什么include()test.php(手动创建)而不是result.php(使用fwrite() 创建)中工作,而两者都在同一个文件夹中?

我所知道的文件之间的唯一区别:

  1. 所有者可能不同(result.php 不会由用户 nobody 创建吗?)
  2. 权限最初不同。 FTP 文件(工作)是 0775,而使用 fwrite() 创建的文件(包括不工作)有 664,并由 generator.php chmoded 到 0775
  3. 工作中的 test.php 文件是在 Mac 上使用 Smultron 编辑并通过 FTP 上传的,而 result.php 是由fwrite() 在 Linux 上的 generator.php 创建的,从浏览器调用。

【问题讨论】:

  • 文件执行前是否关闭?另外,我们能看到这两个生成的文件吗?
  • 您是否尝试过使用 FTP 浏览器下载 result.php 并手动检查它们是否相同?另外,您在访问 result.php 时(无论是在页面上还是在日志中)是否会遇到任何错误?
  • 是的,请确保在尝试执行之前调用 fclose($fp)。同时ftp输出文件并手动检查内容。
  • 是的,result.php 文件会在很久以后手动执行(通过浏览器)。通过 FTP 检查内容似乎相同。
  • 我实际上从 result.php 中剪下了 2 行代码(通过 FTP 获取)并创建了 test.php.. 另外,如果我将 'include' 替换为 'require' 那么它只是停在那句话上。

标签: php permissions include php4 fwrite


【解决方案1】:

当 PHP4 安全模式开启时,result.php 被另一个 uid 写入,无法访问包含的文件,该文件属于另一个 uid。

安全模式限制生效。 uid为48的脚本不允许访问 /var/www/vhosts/example.com/httpdocs/ads/sidebar_top.php 归 uid 10010 所有 在 /var/www/vhosts/example.com/httpdocs/results/result.php 第 130 行

我通过打开 php.ini 并更改为 safe_mode_gid = On 并将我的包含目录添加到 safe_mode_include_dir 来解决此问题。

我还必须重新启动 Apache 才能让更改生效。

【讨论】:

    【解决方案2】:
    fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"); ?>' . "\n");
    

    我认为你有一个额外的 "

    【讨论】:

    • 这是一个多余的串联,但我认为它与问题无关。
    猜你喜欢
    • 2019-06-15
    • 2022-10-23
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多