/    Sign up×
Community /Pin to ProfileBookmark

headers and footers

Howdy there. I’m a noob to php. I’m learning specifically for this purpose. I want to make a header and footer on each of my pages, but I want to just have to edit it once to change all of them. I want to set some variable, which is a bunch of html that equals the header, and then at the top of each page I just tell it to show that piece of code using the variable. How would I go about doing this? I can explain my question further if it’s necessary.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@CharlesNov 04.2006 — Unless you are using a great deal of PHP on the page it's better to just save each in its own file as an HTML fragment and then include it with something like:[code=php]<?php include ($_SERVER['DOCUMENT_ROOT'].'/head.inc') ?>[/code]
Copy linkTweet thisAlerts:
@bko376authorNov 04.2006 — Why the .inc file extension instead of .php? I've never seen it.
Copy linkTweet thisAlerts:
@CharlesNov 04.2006 — You may use any extension that excites you.

I use ".inc" for includes for the obvious reason but also because I block HTTP access to any files that match "*.in?". That hides my includes and my ".ini" files.
Copy linkTweet thisAlerts:
@bko376authorNov 04.2006 — Oh alright. Sounds good. Thanks a lot.
Copy linkTweet thisAlerts:
@bko376authorNov 05.2006 — Problem.

This code is in www/bko376.awardspace.com/home.php

[code=html]<?php include ($_SERVER['DOCUMENT_ROOT'].'header.php') ?> [/code]

This code is in www/bko376.awardspace.com/header.php

[code=html]<center>
<?php echo '<a href="home.php">Home</a> <a href="movies.php">Movies</a> <a href="art.php">Art</a>'; ?>
</center>[/code]


yet I get this message when trying to load home.php
Warning: main(/header.php): failed to open stream: No such file or directory in /home/www/bko376.awardspace.com/home.php on line 9

Warning: main(): Failed opening '/header.php' for inclusion (include_path='.:/usr/local/php4/share/pear') in /home/www/bko376.awardspace.com/home.php on line 9[/quote]
Copy linkTweet thisAlerts:
@NogDogNov 05.2006 — I think you just need to add a slash for the directory separator (as in Charles' example):
<i>
</i>&lt;?php include ($_SERVER['DOCUMENT_ROOT'].'[color=red][b]/[/b][/color]header.php') ?&gt;
Copy linkTweet thisAlerts:
@bko376authorNov 05.2006 — Didn't help. And neither did using the full address.
Copy linkTweet thisAlerts:
@pcthugNov 05.2006 — I think as you're on free hosting there may be several account's per server and thus Document Root. Try the following instead:
[code=php]
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'header.php';
if(!@include($file))
die("Could not include file: <em>$file</em>");
[/code]
Copy linkTweet thisAlerts:
@bko376authorNov 05.2006 — Hooray! Works very well, thank you.
×

Success!

Help @bko376 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...