PHP Header and Footer

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
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

PHP Header and Footer

Post by lmg »

I have created a series of pages for which I would like to have a similar header/footer. The footer is going to be the same on all of the pages, but I would like to change the title for the header. For example, I would like a page titled 'Login' and one titled 'Main Menu.' Is there a way I can pass an argument to the header and have it output the name I specify?

Here is my header code so far:

Code: Select all

<html>
<head>
<title><?php echo "name goes here";?></title>
</head>
 
<h1><center><font face="Trebuchet MS"><?php echo "name goes here"; ?></font></center></h1>
<hr>
<br>
</html>
I would like to replace the "name goes here" part to a variable, but I am unsure how to pass the variable from the main page to the header.
snarkiest
Forum Commoner
Posts: 30
Joined: Mon May 04, 2009 10:06 am
Location: Latvia
Contact:

Re: PHP Header and Footer

Post by snarkiest »

You can separate the header in parts.

header1.html

Code: Select all

<html>
<head>
<title>

header2.html

Code: Select all

</title>
</head>
header3.html

Code: Select all

<h1><center><font face="Trebuchet MS">
header4.html

Code: Select all

</font></center></h1>
<hr>
<br>
</html>
Then in each page where you header is write:

Code: Select all

<?PHP 
include("header1.html");
echo $yourvariable;
include("header2.html");
And where footer is write:

Code: Select all

<?PHP
include("header3.html");
echo $yourvariable;
include("header4.html");
?>
Last edited by snarkiest on Mon Jun 01, 2009 4:19 pm, edited 1 time in total.
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: PHP Header and Footer

Post by lmg »

Good call - thanks!

Is there a way to do it all in one file though?
snarkiest
Forum Commoner
Posts: 30
Joined: Mon May 04, 2009 10:06 am
Location: Latvia
Contact:

Re: PHP Header and Footer

Post by snarkiest »

Yes, create a file vartext.php.

Code: Select all

 
<?PHP
$header1 = "<html><head><title>";
$header2 = "</title></head>";
$header3 = "<h1><center><font face='Trebuchet MS'>"; //code between "" can't contain again " ", so you have to use '. 
$header4 = "</font></center></h1><hr><br></html>";
$yourvariable = "Untitled";
?>
 
Then in each page:

Code: Select all

 
<?PHP
include("vartext.php"); //this should before all code.
 
echo $header1;
echo $yourvariable; //you can add more variables to vartext.php and in each page just change this variable name. i hope you understand.
echo $header2;
 
echo $header3;
echo $yourvariable;
echo $header4;
?>
 
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: PHP Header and Footer

Post by lmg »

Thanks again :)

Do you think it is unclear if I write my code like this:

header.php

Code: Select all

 
<?php
    $header1 = "<html><head><title>";
    $header2 = "</title></head><h1><center><font face='Trebuchet MS'>";
    $header3 = "</font></center></h1><hr><br></html>";
?>
 
question.php line which prints header

Code: Select all

 
include("header.php");
echo $header1."Question".$header2."Question".$header3;
snarkiest
Forum Commoner
Posts: 30
Joined: Mon May 04, 2009 10:06 am
Location: Latvia
Contact:

Re: PHP Header and Footer

Post by snarkiest »

lmg wrote:

Code: Select all

 
include("header.php");
echo $header1."Question".$header2."Question".$header3;
Hm, I don't know if this way it is going to work. Does it?
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: PHP Header and Footer

Post by lmg »

It sure does. I made some changes and now I have it working like this:

Code: Select all

 include("header.php");
    $name="Login";
    echo $header1.$name.$header2.$name.$header3;
Turv
Forum Commoner
Posts: 25
Joined: Fri Mar 13, 2009 3:56 pm

Re: PHP Header and Footer

Post by Turv »

index.php

Code: Select all

 
<?php
$Title = "Your Title";
$MetaKeywords = "Keywords,Go,Here";
$MetaDescription = "Page Description";
 
// Include Header
include("header.php");
?>
<!-- Content goes here -->
<?php
// Include Footer
include("footer.php");
?>
 
header.php

Code: Select all

 
<html>
<head>
<title><?php echo $Title;?></title>
<meta name="keywords" content="<?php echo $MetaKeywords; ?>" />
<meta name="description" content="<?php echo $MetaDescription; ?>" />
</head>
  
<h1><center><font face="Trebuchet MS"><?php echo $Title; ?></font></center></h1>
<hr>
<br>
 
footer.php

Code: Select all

 
</body>
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: PHP Header and Footer

Post by mattpointblank »

The post above is the best suggestion so far. Similar to that, I run my header as a function sometimes if the site structure is simple and doesn't change much:

Code: Select all

 
function newPage($title, $description, $keywords) {
   $pagetitle = $title;
   $pagedescription = $description;
   $pagekeywords = $keywords;
   include 'header.php';
}
 
Then within header.php I have variables like

Code: Select all

 
<title><?php echo $pagetitle; ?></title>
 
So my pages look like:

Code: Select all

 
newPage("My home page", "description of page goes here", "page, keywords, go, here");
 
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: PHP Header and Footer

Post by lmg »

Thanks matt and Turv. That is EXACTLY what I was looking for!
Post Reply