1

(Please be patient, this does have something to do with include.) I am waiting for a domain to transfer over and am trying to set it up on the new hosting service ahead of time. I realized that on the old site all the path names were absolute, so all my links on the new host point to pages on the old host. I decided to make them all relative (for future possible moves also). I first did it like this:

index.php

include ('./header.php');

header.php

include "./panel.php";

panel.php

Contents of panel.

This works, and my page displays:

Contents of panel.

Then I decided to set a variable for the domain because I want to include this header file from files in subdirectories and I can use the domain variable to make an absolute path. Right now I have a temporary domain name, which I can change later to the real domain name when the transfer comes through. So I changed header.php to:

$domain="http://tempdomain.com"; //I can change this after the transfer 
$panel=$domain."/panel.php";
echo $panel;
if ((include $panel) !== 1)
{
  echo "<br>include failed";
}

What I get is:

http://tempdomain.com/panel.php
include failed

I've looked at various sites for include syntax, but I can't find any error in my code. All these files are in the / directory. Any ideas?

5
  • 1
    I hope you understand differences between file-system and accessing files over http protocol. :) I.e. to include scripts you should use physical path on file-system. Commented Mar 16, 2013 at 13:04
  • Add double quotes around the if((include "$panel") !== 1). And see what happens Commented Mar 16, 2013 at 13:06
  • @Ranjith It does not matter. Commented Mar 16, 2013 at 13:08
  • @PLB : Whats the solution for that code.. Commented Mar 16, 2013 at 13:11
  • @Ranjith There're two solutions I can think of. First, use paths on file-system (normally, everybody should be doing it). Second, get contents over http using file_get_contents and use eval to execute that script. The second method will work but it should be never used! Commented Mar 16, 2013 at 13:14

3 Answers 3

1

When you include, you have to give the directory structured, not the url.

Your hosting server path may be home/public/www/htdocs/your_directory_name/panel.php something like this. Then it will work.

Sign up to request clarification or add additional context in comments.

2 Comments

If I understand you correctly, I should forget the domain variable and change my code in header.php to: if ((include "/panel.php") !== 1) { echo "<br>include failed"; } I just tried this and still got include failed. What am I missing?
Try with this, include($_SERVER['DOCUMENT_ROOT'].'/header.php');
1

remort include is also posiible if 1. server's php.ini should allow it. 2. the file which will be included should not be preprossed before include. That means it must return unprocessed code :)

Comments

0
  • First, the allow_url_fopen flag must be set in php.ini. Otherwise remote include() cannot be done. Run var_dump(ini_get("allow_url_fopen")); to see if it is the case.

    • Second, "Windows versions of PHP prior to PHP 4.3.0 do not support access of remote files via this function, even if allow_url_fopen is enabled." - see PHP docs

    • Third, your remote PHP script must produce valid PHP code as output. If you include() via http, then not the script itself, but its output will be included.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.