简体   繁体   中英

How to include a php file in subdomain from main domain

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

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~

Here is a tidy way to do this.

An include from a subdomain:

include( $_SERVER['DOCUMENT_ROOT'] . "/../exampledomain.com/filetoinclude.php" ); 

This gets the whole path to your file, and then regardless of the subdomain you are in, it will jump up to the folder above that and call "exampledomain.com" domain. This saves showing your folder hierarchy to anyone who sees that file.

You could also reverse this an include to a subdomain:

include( $_SERVER['DOCUMENT_ROOT'] . "/../sub.exampledomain.com/filetoinclude.php" ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM