PHP4 在 fwrite() 创建的文件中使用 include() 时出现问题

发布于 2024-08-11 11:23:07 字数 1839 浏览 9 评论 0原文

我有一个名为 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 -->

但是当我访问 时,include() 语句不起作用result.php 在浏览器中。 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,并由 生成器进行 chmoded。 php0775
  3. 工作 test.php 文件是在 Mac 上使用 Smultron 进行编辑并通过 FTP 上传的,而 result.php 是通过 fwrite() 在 < Linux 上的 code>generator.php,从浏览器调用。

I have a file called generator.php that uses fwrite() to create a result.php on the server (Apache, PHP4).

One of the lines in result.php is a PHP include() statement.

So, in 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);  

And in 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 -->

But that include() statement doesn't work when I visit result.php in a browser. The echo statement does, so I know the path is correct.

Another test.php with the same code, which I uploaded using FTP into the same folder, works fine.

The code in the same in both files, when recovered via FTP.

In test.php: (works, echoes and includes correctly.)

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

Any idea why the include() is working in test.php (created manually) and not in result.php (created using fwrite()), when both are in the same folder?

The only differences I know of between the files:

  1. Owner could be different (wouldn't result.php be created by user nobody?)
  2. Permissions are originally different. FTP'd file (working) is 0775, while the ones created using fwrite() (include not working) had 664, and is chmoded by the generator.php to 0775.
  3. Working test.php file was edited on a Mac with Smultron and uploaded via FTP, while result.php was created by fwrite() in generator.php on Linux, called from a browser.

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

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

发布评论

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

评论(2

时光沙漏 2024-08-18 11:23:07
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"); ?>' . "\n");

我认为你那里有一个额外的“

fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"); ?>' . "\n");

you had an extra " in there i think

别忘他 2024-08-18 11:23:07

当 PHP4 安全模式打开时,由另一个 uid 写入的 result.php 无法访问属于另一个 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 才能使更改生效。

When PHP4 safe mode is on, the result.php, being written by another uid, cannot not access the included file, which belongs to another uid.

SAFE MODE Restriction in effect. The script whose uid is 48 is not allowed to access
/var/www/vhosts/example.com/httpdocs/ads/sidebar_top.php owned by uid 10010
in /var/www/vhosts/example.com/httpdocs/results/result.php on line 130

I resolved this by opening php.ini and changing to safe_mode_gid = On, and adding my includes directory to safe_mode_include_dir.

I also had to restart Apache to let the changes take effect.

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