如何在主域的子域中包含php文件 [英] How to include a php file in subdomain from main domain

查看:63
本文介绍了如何在主域的子域中包含php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器中有两个文件夹:

I have two folders in my server:

--MainDomain
  -header.php
  -index.php
  -footer.php
--Subdomain
  -index.php

现在,我想将主域中的header.php包含在子域中的index.php中.

Now I want to include header.php from main domain in index.php which is in sub domain.

通过使用include函数,我得到了错误

By using the include function I get the error

无法打开流:/home/content/xx/xxx/xxxx/xxx/

failed to open stream: No such file or directory in /home/content/xx/xxx/xxxx/xxx/

我知道,当在给定的提到的路径上不存在文件时,会发生此错误,但是如何从主域到子域包含一个文件? 预先谢谢你

I know that this error occurs when a file does not exists on the given mentioned path, but how can I include a file from main domain to sub domain? thank you in advance

推荐答案

我知道这个问题是八个月前提出的,但是我刚刚遇到了同样的问题,并且已经解决了.希望这将对将来遇到相同问题的人们有所帮助.

I know this question was asked eight months ago, but I've just had this same exact problem and solved it. Hopefully this will help people in the future who stumble across the same thing.

  • 例如,假设您有两个子域:胡萝卜.cake.com和Chocolate.cake.com
  • 您在胡萝卜子域中有一个文件:胡萝卜.cake.com/scripts/login.php
  • 您在Chocolate子域中有一个文件:Chocolate.cake.com/index.php
  • 您要在文件"index.php"中包含文件"login.php".

重要的是,这是可能的,但是只有将两个域的文件都存储在一个服务器文件系统中才有可能.

The important part is that this IS possible but it's possible ONLY if you do store the files of both of these domains on one single server filesystem.

将以下代码添加到index.php文件中:

Add the following code to your index.php file:

    <?php 
echo '<br>'.$_SERVER['DOCUMENT_ROOT'].'<br>';
    ?>

然后在浏览器中加载index.php文件.它将显示您的实际目录文件夹是什么.例如,当我这样做时,它表明我的文件系统如下所示:

And then go load up your index.php file in a browser. It will show you what your actual directory folders are. For example, when I did this, it showed me that my filesystem looked like:

/home3/cake/public_html/chocolate_domain

您将与我的不同,所以请自己测试一下php代码片段.然后立即删除该代码.它仅用于测试目的.但是现在您知道了如何实际访问所有域中的所有文件夹.这意味着您的index.php文件位于例如

Yours will be DIFFERENT FROM MINE, so test that php code snippet out, yourself. And delete that code right afterwards. It is just for testing purposes. But now you know how to actually access all the folders to all your domains. This means that your index.php file is in, for example,

/home3/cake/public_html/chocolate_domain/index.php

,例如,您的login.php文件位于

and that your login.php file is in, for example,

/home3/cake/public_html/carrot_domain/scripts/login.php

现在,最后的结果!如果要在"index.php"中包含"login.php",则应将以下代码放入"index.php"文件中:

Now, for the final result! If you want to include "login.php" inside of "index.php", then you should put the following code into your "index.php" file:

    <?php 
include("/home3/cake/public_html/carrot_domain/scripts/login.php"); 
    ?>

请再次记住,您的实际路径是不同的,因此请使用文档根目录的回显对其进行测试,就像我之前说的那样!

Please remember, once again, that the actual path for YOU is different, so test it out using the echo of the document root, like I said before~

祝你好运〜

这篇关于如何在主域的子域中包含php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆