How to include a php file in subdomain from main domain

╄→尐↘猪︶ㄣ 提交于 2019-12-12 13:44:45

问题


I have two folders in my server:

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

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

By using the include function I get the error

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


回答1:


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.

  • For example, let's say you have two subdomains: carrot.cake.com and chocolate.cake.com
  • You have a file in the carrot subdomain: carrot.cake.com/scripts/login.php
  • And you have a file in the chocolate subdomain: chocolate.cake.com/index.php
  • You want to include the file "login.php" inside of the file "index.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.

Add the following code to your index.php file:

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

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

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

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

/home3/cake/public_html/carrot_domain/scripts/login.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~

good luck~



来源:https://stackoverflow.com/questions/23210352/how-to-include-a-php-file-in-subdomain-from-main-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!