include(); problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

include(); problem

Post by someberry »

Hi,

Whenever I try and do:

Code: Select all

include('http://www.domain.com/header.php');
It adds all of the layout styles, but seems to ignore the PHP in that file. If I do:

Code: Select all

include('header.php');
Everything works fine. However, I need to use the full URL, as I need to call the header from other directories (hence cannot use the local address), do I need to set an include path before executing the include() to make it work correctly?

Thanks for the help,
Someberry.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't need to use a URL, which will generate a subrequest and have php process that file returning the results to this script which will process it. Relative or absolute paths are preferred (or potentially setting include_path).

../sibling/child/script.php will look for script.php in the parent directory's 'child' directory under the 'sibling' directory, for instance.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

you could also use $_SERVER['DOCUMENT_ROOT']; to set a path.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

Ok, I managed to get it working using the ../../ technique - thanks for that. However, I have stumbled upon another problem.

I want to include a header, that header has the session_start(); in it. As you well know, session_start(); has to be called first so at the top of the index.php script, I have:

Code: Select all

<?PHP include('../../test/header.php'); ?>
However, I am using this as a test directory for the new layout which I am going to transfer in a couple of weeks. As it is, it isnt very directory friendly, when I transfer the files, I am going to have to change every file so that it looks in the right directory for the header and footer files. Is there anyway that I can get the path leading up to, but not including the file name, so for instance:

/home/my_space/public_html/test/

I looked through the $_SERVER variables, but it doesnt appear to be much use. Is there anyway I can get it, or anyway I can make the script in Apache, and then use it that way?

Thanks,
Someberry.
Post Reply