5

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

Prix
  • 19,417
  • 15
  • 73
  • 132
narendar
  • 63
  • 2
  • 7
  • What is the full path to both index.php files? `/home/content/maindomain/index.php` and `/home/content/subdomain/index.php`? – Prix Apr 22 '14 at 04:27
  • main domain path : /home/content/xxxxxxxxxx/maindomain/index.php – narendar Apr 22 '14 at 04:29
  • subdomain path : /home/content/xxxxxxx/subdomain/index.php – narendar Apr 22 '14 at 04:30
  • The `include` you tried on the `index.php` of the subdomain was like this? `include '/home/content/xxxxxxxxxx/maindomain/header.php';` – Prix Apr 22 '14 at 04:32
  • yes i did it by giving full path and "../" using this – narendar Apr 22 '14 at 04:32
  • And they both result on the same error message? – Prix Apr 22 '14 at 04:33
  • no main domain works fine. error failed to open stream occurs only in subdomain – narendar Apr 22 '14 at 04:34
  • yea i tried both n error was same – narendar Apr 22 '14 at 04:36
  • Could you try this on each index.php and make sure they are both giving you the right path for main and sub folders `echo dirname(__FILE__);` – Prix Apr 22 '14 at 04:37
  • yes they both are giving same path when in include a file from a folder named "inc"... and its jst showing a ".(dot)" for normal file – narendar Apr 22 '14 at 04:42
  • Sorry can you explain better not sure what you're saying on your previous message. – Prix Apr 22 '14 at 04:44
  • my main domain contains a "inc" folder which holds some files.. when i include a particular file from "inc" .. lets suppose am including "inc/header.php" in main domain and "inc/header" in subdomain echo dirname(relativepath."inc/header") displays same full path for main domain and subdomain – narendar Apr 22 '14 at 04:50
  • does the header file have the same user and group as the index.php file on the subdomain folder? Is the subdomain on a different virtualhost on your apache config? Do they use different username/group? – Prix Apr 22 '14 at 04:52
  • no they belong to same username and on same host – narendar Apr 22 '14 at 04:54
  • Well based on all the information provided I can only think on 2 things, 1) the file index.php does not have access to include it, 2) the folder the file is does not match the folder you're trying to call it, but perhaps some one have other ideas of what could be it. – Prix Apr 22 '14 at 04:56
  • may be i have to check httpd.conf file – narendar Apr 22 '14 at 04:58
  • Check this http://stackoverflow.com/questions/13577189/php-include-a-file-at-subdomain-from-main-domain-on-a-different-server-host – Nadeem Khan Apr 22 '14 at 05:45
  • can we fix that error using .htaccess – narendar Apr 22 '14 at 05:48

2 Answers2

12

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~

cake
  • 658
  • 5
  • 13
0

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" ); 
Paul V
  • 351
  • 3
  • 9