14 replies [Last post]
Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 5 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

I'm calling a function contained in header.php which prints the header on each page:

<?php include ("$docRoot./includes/header.php"); 
printHeader("","",""); ?>

(The variables are empty at the moment but will be filled later to pass title, description etc. to each page.)

header.php starts off with:

<? $docRoot = $_SERVER['DOCUMENT_ROOT'];
Function printHeader($pageTitle,$pageDescription,$bodyID) { ?>

then the HTML.
My problem is, on the site index page it works fine, but when called from a page in a sub-directory, I get Fatal error: Call to undefined function printHeader().
So my question is, why doesn't it work for the sub-directory page and how does it actually work on the index page when the page calls $docRoot but that variable's defined first in the include that the variable is calling in? :?

EDIT: By the way, replacing $docRoot with $_SERVER['DOCUMENT_ROOT'] in the call to the include doesn't work either.

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

Tags:
roytheboy
roytheboy's picture
Offline
Guru
North Wales, UK
Last seen: 6 years 17 weeks ago
North Wales, UK
Timezone: GMT+1
Joined: 2004-09-18
Posts: 2233
Points: 41

$_SERVER['DOCUMENT_ROOT'] problem

You seem to have answered your own question with regards to the variable $docRoot being defined within the very document that it calls ( :? ), but WRT the include() function, the syntax needs to be one of these two:

<?php include ($docRoot."/includes/header.php");

<?php include ("$docRoot/includes/header.php");

or better still:

<?php include ($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

As to why it currently works on the index page - if you take away the undefined variable ($docRoot) then the remaining path obviously points to your header file.

Life's a b*tch and then you die!

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 5 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

$_SERVER['DOCUMENT_ROOT'] problem

roytheboy wrote:

or better still:
<?php include ($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

No, I've tried that and it doesn't work. If I change the path to ("../includes/header.php"), it's fine, but it doesn't like the DOCUMENT_ROOT.

roytheboy wrote:

As to why it currently works on the index page - if you take away the undefined variable ($docRoot) then the remaining path obviously points to your header file.

If you take away the variable, what you're left with is an absolute path aren't you - ("/includes/header.php")? And it doesn't work for that either.

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 8 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

$_SERVER['DOCUMENT_ROOT'] problem

No. Roy's point is the "." it shouldn't be there inside the string, its only needed when you concatentate strings not when you are using variable substitution inside a string.

if you take away $docroot, you are left with "./include/headers.php", which means relative to the current directory.

What is your actual directory structure?
Print out the values of the variables so you can see what is happening.

Also, php defines a "__FILE__" variable which is the file system location of the current file. dirname(__FILE__) will give you the directory of the current file. Paths constructed around $_SERVER['DOCUMENT_ROOT'] are not always reliable - e.g. in apache when an "Alias" has been used to map different parts of the file system into the site structure.

/edit removed some abiguities.

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 5 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

$_SERVER['DOCUMENT_ROOT'] problem

Chris..S wrote:
Paths constructed around $_SERVER['DOCUMENT_ROOT'] are not always reliable - e.g. in apache when an "Alias" has been used to map different parts of the file system into the site structure.

Given my limited experience with PHP so far, I just assumed that $_SERVER['DOCUMENT_ROOT'] was the definitive way of setting absolute include paths. But it's definitely not working in my case so I guess I'll stick with relative paths instead.

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 8 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

$_SERVER['DOCUMENT_ROOT'] problem

If you know there are no alias entries present or likely $_SERVER['DOCUMENT_ROOT'] is fine. It is guaranteed to return the absolute file system path to the site's document root folder. After that its simple string manipulation/concatenation to get the final file path that you are after.

In this case only you know your site structure so its pretty difficult to suggest where you may be going wrong.

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 5 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

$_SERVER['DOCUMENT_ROOT'] problem

Chris..S wrote:
In this case only you know your site structure so its pretty difficult to suggest where you may be going wrong.

It's pretty simple so far:
index.php is in the top level with all other folders including /includes/
Each section of the site will have its own subfolder, e.g. I'm working on the privacy page at the moment, so /privacy/index.php.
So the privacy page's path to the includes folder is ../includes/
All the other sections will work the same way.
I've tried calling the include with include ($_SERVER['DOCUMENT_ROOT']."/includes/header.php"); but I get the error.

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

roytheboy
roytheboy's picture
Offline
Guru
North Wales, UK
Last seen: 6 years 17 weeks ago
North Wales, UK
Timezone: GMT+1
Joined: 2004-09-18
Posts: 2233
Points: 41

$_SERVER['DOCUMENT_ROOT'] problem

Chris..S wrote:
No. Roy's point is the "." it shouldn't be there inside the string, its only needed when you concatentate strings not when you are using variable substitution inside a string.

Thanks Chris - I've been a bit busy this morning!

Tyssen - given that your site is so logically structured, why not just make life easy for yourself and use this in every page in a subdirectory:

<?php include ("../../includes/header.php");

...and this on your index page:

<?php include ("../includes/header.php");

Life's a b*tch and then you die!

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 5 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

$_SERVER['DOCUMENT_ROOT'] problem

roytheboy wrote:
Tyssen - given that your site is so logically structured, why not just make life easy for yourself and use this in every page in a subdirectory:

<?php include ("../../includes/header.php");

...and this on your index page:

<?php include ("../includes/header.php");
That's what I've decided to do even though I'd still like to know why $_SERVER['DOCUMENT_ROOT'] doesn't work in this instance as I've always been used to using absolute rather than relative paths.

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

roytheboy
roytheboy's picture
Offline
Guru
North Wales, UK
Last seen: 6 years 17 weeks ago
North Wales, UK
Timezone: GMT+1
Joined: 2004-09-18
Posts: 2233
Points: 41

$_SERVER['DOCUMENT_ROOT'] problem

It should work but like Chris says, different servers can be configured in different ways. You could try this just in case you've got something weird going on with your quotes:

$docroot = $_SERVER['DOCUMENT_ROOT'];
include ($docroot."/includes/header.php");

...but other than that you'd need to debug it on the server.

edit: you're not using php3 by any chance are you?

Life's a b*tch and then you die!

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 8 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

$_SERVER['DOCUMENT_ROOT'] problem

check the value of $_SERVER['DOCUMENT_ROOT'].

better still stick this file in your docroot while you're developing

phpinfo.php

<?php phpinfo(); ?>

if you ever need to check a setting, version or whether a particular module is installed just visit www.mysite.com/phpinfo

remember to delete it when you go live Wink

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 5 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

$_SERVER['DOCUMENT_ROOT'] problem

Seems it's not just me getting things wrong. I've contacted my ISP and apparently when running PHP under IIS, you have to set a doc_root in php.ini. :roll:

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 4 years 45 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

$_SERVER['DOCUMENT_ROOT'] problem

Oh you know what the guys will say, you omitted an important bit of information there.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 5 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

$_SERVER['DOCUMENT_ROOT'] problem

Hugo wrote:
Oh you know what the guys will say, you omitted an important bit of information there.

You're probably right. :oops:

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

cool
Offline
newbie
Glenrothes, Fife, United Kingdom
Last seen: 11 years 15 weeks ago
Glenrothes, Fife, United Kingdom
Joined: 2008-08-05
Posts: 1
Points: 0

If your using an Apache

If your using an Apache Server. You can use this piece of code. It locates the Pathname of your root directory and you can call any file from there:

// Setting Up Environment Variables:
 
 
define("pathToRoot", dirname(realpath(basename(getenv("SCRIPT_NAME"))));
/* Tells PHP how to find the Linux path to your Script Directory, no matter how complex.
   This call must exist in every file that calls the include or require function
   @ example: include("pathToRoot/includes/header.php");
 
   As you can see, each call locates the home directory of your Website and loads the
   individual files from there on in.
 
   Remember not to set this as a variable, for added security and prevent malicious changes to   
   your underlying OS path, LETS NOT let visitors browse your entire OS!!!
*/
 
echo pathToRoot;