如何在文件根目录外包含文件?

时间:2023-02-05 12:55:47

What I want do to is to include 'file1.php' from 'domain1' into 'file2.php' on 'domain2'. So what I figured I should do is something like this:

我想做的是将'domain1'中的'file1.php'包含在'domain2'上的'file2.php'中。所以我认为我应该做的是这样的:

file2.php
require_once '/var/www/vhosts/domain1/httpdocs/file1.php';

But this won't work for reasons I can't truly grasp. So what I did was to add my path to the include path. Something like:

但这不能用于我无法真正掌握的原因。所以我做的是将我的路径添加到包含路径。就像是:

file2.php
set_include_path(get_include_path() . PATH_SEPARATOR . "/var/www/vhosts/domain1/httpdocs");
require_once 'file1.php';

So can you please give me some hints as of where I'm doing wrong ?

那么请你给我一些提示,说明我做错了什么?

Thanks

UPDATE - Either way I get the following error message:

更新 - 无论哪种方式,我收到以下错误消息:

Fatal error: require() [function.require]: Failed opening required '/var/www/vhosts/domain1/httpdocs/file1.php' (include_path='.:/php/includes:/usr/share/pear/') in /var/www/vhosts/domain2/httpdocs/file2.php on line 4

Also I have tried this both with safe_mode On and Off.

我也尝试过使用safe_mode On和Off。

UPDATE2: Also I've changed the permissions to 777 on my test file and I've double-checked the paths to the include file in bash.

UPDATE2:我还在我的测试文件上将权限更改为777,并且我已经在bash中仔细检查了包含文件的路径。

SOLUTION: I've managed to solve the mystery! My hosting company uses Plesk to manage domains and such. Also the error reporting level in php.ini was not E_ALL. When I set error reporting to E_ALL I got a warning saying:

解决方案:我设法解开了这个谜!我的托管公司使用Plesk来管理域名等。 php.ini中的错误报告级别也不是E_ALL。当我向E_ALL设置错误报告时,我收到一条警告:

Warning: require() [function.require]: open_basedir restriction in effect.

So I went in /var/www/vhosts/domain2/conf/httpd.include and edited the open_basedir path. Note that this is not a durable solution since this config file is rewritten by plesk each time the domain config is changed. What you should do is edit (or create) the 'vhost.conf' file in the same directory and then run:

所以我进入/var/www/vhosts/domain2/conf/httpd.include并编辑了open_basedir路径。请注意,这不是一个持久的解决方案,因为每次更改域配置时,此配置文件都会被plesk重写。您应该做的是在同一目录中编辑(或创建)'vhost.conf'文件,然后运行:

 /usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=DOMAIN.TLD

This should reconfigure the settings for your domain but for some strange reason it won't work with open_basedir. I can modify other things like document_root but it won't change open_basedir, but that's another problem :D

这应该重新配置您的域的设置,但由于一些奇怪的原因,它不适用于open_basedir。我可以修改其他东西,比如document_root,但它不会改变open_basedir,但这是另一个问题:D

SOLUTION FINAL: For those with the same problem here is the final code that worked. I just added this in /var/www/vhosts/domain2/conf/vhost.conf (you can change '/var/www/vhosts' to '/' or anything you like):

解决方案最终:对于那些有同样问题的人来说,最终的代码是有效的。我刚在/var/www/vhosts/domain2/conf/vhost.conf中添加了这个(您可以将'/ var / www / vhosts'更改为'/'或任何您喜欢的内容):

    <Directory /var/www/vhosts/DOMAIN.TLD/httpdocs>
    <IfModule mod_php5.c>
            php_admin_flag engine on
            php_admin_flag safe_mode off
            php_admin_value open_basedir "/var/www/vhosts"
    </IfModule>
            Options -Includes -ExecCGI
    </Directory>

Thank you all guys!

谢谢大家!

7 个解决方案

#1


9  

You can not accomplish this if open_basedir is in effect, which prevents PHP from traversing out of the home directory.

如果open_basedir生效,则无法完成此操作,这会阻止PHP遍历主目录。

What you can do is make sure that docroot1 and docroot2 are owned by users in the same group, set group permissions accordingly and use a symbolic link from docroot2 to docroot1 to read the other web root.

您可以做的是确保docroot1和docroot2归同一组中的用户所有,相应地设置组权限,并使用docroot2到docroot1的符号链接来读取其他Web根目录。

Or, re-build PHP and let it just follow typical *nix permissions like every other process :)

或者,重新构建PHP并让它像其他每个进程一样遵循典型的* nix权限:)

#2


2  

You can include files from anywhere you want, unless your PHP script's permissions, or the safe mode prevent it. Your first approach is perfectly fine. What errors do you get?

您可以在任何地方包含文件,除非您的PHP脚本的权限或安全模式阻止它。你的第一种方法非常好。你得到了什么错误?

Re the comments, that seem to confirm that there is no access from within PHP to a file that definitely exists. As to what it could be, Suhosin having been ruled out, the only thing I can think of is PHP or Apache being some kind of a a chroot Jail:

重新评论,这似乎证实了PHP内部无法访问确实存在的文件。至于它可能是什么,Suhosin已被排除,我唯一能想到的是PHP或Apache是​​某种chroot*:

The main benefit of a chroot jail is that the jail will limit the portion of the file system the daemon can see to the root directory of the jail. Additionally, since the jail only needs to support Apache, the programs available in the jail can be extremely limited. Most importantly, there is no need for setuid-root programs, which can be used to gain root access and break out of the jail.

chroot jail的主要好处是jail会将守护进程可以看到的文件系统部分限制在jail的根目录中。此外,由于jail只需要支持Apache,因此*中可用的程序可能非常有限。最重要的是,不需要setuid-root程序,可用于获取root访问权并打破*。

I have never worked with anything like this so I can't tell you how to spot it (apart from doing a glob() on /var/www/vhosts and see what comes up. But I think this would have to have been set up by an administrator. Who runs your machine?

我从来没有使用过这样的东西,所以我不能告诉你如何发现它(除了在/ var / www / vhosts上做一个glob(),看看会发生什么。但我认为这必须设置管理员。谁运行你的机器?

#3


2  

This works on a couple of machines i manage

这适用于我管理的几台机器

ini_set("include_path",".:/hsphere/local/home/user_name/other_domain.com");
require "filename.php";

#4


1  

I sit here wondering why You didn't jus do a symlink. Or did I mis something? You could symlink a folder with needed includes to the path You have access to.

我坐在这里想知道为什么你没有做一个符号链接。或者我错了什么?您可以将包含所需包含的文件夹符号链接到您有权访问的路径。

#5


0  

Try chmod 777 on a test php file to see if that works, if it does you have permission issues. Also do a simple phpinfo() and see if save mode is on.

在测试php文件上尝试chmod 777以查看是否有效,如果它有权限问题。还要做一个简单的phpinfo()并查看是否启用了保存模式。

#6


0  

what you are using is bad practice. put the dependent code into domain specific. Duplicating the same code is the right approach as it does not affect the operation of both sites.

你正在使用的是不好的做法。将依赖代码放入特定于域的。复制相同的代码是正确的方法,因为它不会影响两个站点的操作。

Try creating the symlink of file1.php and included that as if it is from the local directory.

尝试创建file1.php的符号链接,并将其包含在本地目录中。

also make sure that .htaccess has the followsymlink option set to true

还要确保.htaccess将followsymlink选项设置为true

how about trying this in your file2.php in domain2?

如何在domain2中的file2.php中尝试这个?

require_once '../../../domain1/httpdocs/file1.php';

#7


0  

What happens if you try to require a different file:

如果您尝试要求不同的文件会发生什么:

// test.php
<?php
   echo 'Hello World';
?>

// your file
require_once('test.php');

Does that work? If so, put test.php in the other location and try it again. Does it still work?

那样有用吗?如果是这样,请将test.php放在其他位置并再次尝试。它仍然有效吗?

#1


9  

You can not accomplish this if open_basedir is in effect, which prevents PHP from traversing out of the home directory.

如果open_basedir生效,则无法完成此操作,这会阻止PHP遍历主目录。

What you can do is make sure that docroot1 and docroot2 are owned by users in the same group, set group permissions accordingly and use a symbolic link from docroot2 to docroot1 to read the other web root.

您可以做的是确保docroot1和docroot2归同一组中的用户所有,相应地设置组权限,并使用docroot2到docroot1的符号链接来读取其他Web根目录。

Or, re-build PHP and let it just follow typical *nix permissions like every other process :)

或者,重新构建PHP并让它像其他每个进程一样遵循典型的* nix权限:)

#2


2  

You can include files from anywhere you want, unless your PHP script's permissions, or the safe mode prevent it. Your first approach is perfectly fine. What errors do you get?

您可以在任何地方包含文件,除非您的PHP脚本的权限或安全模式阻止它。你的第一种方法非常好。你得到了什么错误?

Re the comments, that seem to confirm that there is no access from within PHP to a file that definitely exists. As to what it could be, Suhosin having been ruled out, the only thing I can think of is PHP or Apache being some kind of a a chroot Jail:

重新评论,这似乎证实了PHP内部无法访问确实存在的文件。至于它可能是什么,Suhosin已被排除,我唯一能想到的是PHP或Apache是​​某种chroot*:

The main benefit of a chroot jail is that the jail will limit the portion of the file system the daemon can see to the root directory of the jail. Additionally, since the jail only needs to support Apache, the programs available in the jail can be extremely limited. Most importantly, there is no need for setuid-root programs, which can be used to gain root access and break out of the jail.

chroot jail的主要好处是jail会将守护进程可以看到的文件系统部分限制在jail的根目录中。此外,由于jail只需要支持Apache,因此*中可用的程序可能非常有限。最重要的是,不需要setuid-root程序,可用于获取root访问权并打破*。

I have never worked with anything like this so I can't tell you how to spot it (apart from doing a glob() on /var/www/vhosts and see what comes up. But I think this would have to have been set up by an administrator. Who runs your machine?

我从来没有使用过这样的东西,所以我不能告诉你如何发现它(除了在/ var / www / vhosts上做一个glob(),看看会发生什么。但我认为这必须设置管理员。谁运行你的机器?

#3


2  

This works on a couple of machines i manage

这适用于我管理的几台机器

ini_set("include_path",".:/hsphere/local/home/user_name/other_domain.com");
require "filename.php";

#4


1  

I sit here wondering why You didn't jus do a symlink. Or did I mis something? You could symlink a folder with needed includes to the path You have access to.

我坐在这里想知道为什么你没有做一个符号链接。或者我错了什么?您可以将包含所需包含的文件夹符号链接到您有权访问的路径。

#5


0  

Try chmod 777 on a test php file to see if that works, if it does you have permission issues. Also do a simple phpinfo() and see if save mode is on.

在测试php文件上尝试chmod 777以查看是否有效,如果它有权限问题。还要做一个简单的phpinfo()并查看是否启用了保存模式。

#6


0  

what you are using is bad practice. put the dependent code into domain specific. Duplicating the same code is the right approach as it does not affect the operation of both sites.

你正在使用的是不好的做法。将依赖代码放入特定于域的。复制相同的代码是正确的方法,因为它不会影响两个站点的操作。

Try creating the symlink of file1.php and included that as if it is from the local directory.

尝试创建file1.php的符号链接,并将其包含在本地目录中。

also make sure that .htaccess has the followsymlink option set to true

还要确保.htaccess将followsymlink选项设置为true

how about trying this in your file2.php in domain2?

如何在domain2中的file2.php中尝试这个?

require_once '../../../domain1/httpdocs/file1.php';

#7


0  

What happens if you try to require a different file:

如果您尝试要求不同的文件会发生什么:

// test.php
<?php
   echo 'Hello World';
?>

// your file
require_once('test.php');

Does that work? If so, put test.php in the other location and try it again. Does it still work?

那样有用吗?如果是这样,请将test.php放在其他位置并再次尝试。它仍然有效吗?