/    Sign up×
Community /Pin to ProfileBookmark

First time using include statement and not working properly!

Hi all,

Bare with me as I’m very new to php but this issue should be easy for the gurus.

So, I have a dynamic web template which has an ‘include’ command reference to two files located in a folder called ‘includes’ at root/includes/ to create a common header and footer to all pages that this template is applied to. Within the dynamic web template, which sits in the root of the site, are the lines

<?php
$path = $_SERVER[‘DOCUMENT_ROOT’];
$path .= “/includes/ztest_hinclude.php”;
include($path);

?>

for the header and

<?php
$path = $_SERVER[‘DOCUMENT_ROOT’];
$path .= “/includes/ztest_finclude.php”;
include($path);

?>

for the footer.

If I apply this dynamic web template to any page in the root of the site, any hyperlinks to pages contained within the referenced files to be included are correct (e.g. the link to the home page shows as root/index.php)

However, if I create a folder in the root, call it ‘myfolder’ for example, save a php file into that folder and apply the dynamic web template to it, the links referenced in the two files to be included are incorrect (e.g. instead of pointing to the home page at root/index.php it points to root/myfolder/index.php which is clearly wrong).

The two include statements above have been sourced from the web and this seems to be the acccepted method of determining the root of the site so I can’t understand quite where I’ve gone wrong. Any advice would be much appreciated.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@eyetranceauthorNov 11.2013 — Apparently the solution is not to link to index.php but /index.php so if the template is applied to any file in a sub-folder, the path to index.php is reset back to the document root.
Copy linkTweet thisAlerts:
@NogDogNov 11.2013 — Note that you are specifying a [i]file system[/i] path, which means that "/includes/ztest_hinclude.php" would be relative to the root directory ("/") on the disk, not your web site root. You could have instead used:
[code=php]
include $_SERVER['DOCUMENT_ROOT']."/includes/ztest_hinclude.php";
[/code]
×

Success!

Help @eyetrance spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...