Page 1 of 1

PHP Header and Footer

Posted: Mon Jun 01, 2009 4:03 pm
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.

Re: PHP Header and Footer

Posted: Mon Jun 01, 2009 4:10 pm
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");
?>

Re: PHP Header and Footer

Posted: Mon Jun 01, 2009 4:19 pm
by lmg
Good call - thanks!

Is there a way to do it all in one file though?

Re: PHP Header and Footer

Posted: Mon Jun 01, 2009 4:24 pm
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;
?>
 

Re: PHP Header and Footer

Posted: Mon Jun 01, 2009 4:32 pm
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;

Re: PHP Header and Footer

Posted: Mon Jun 01, 2009 4:44 pm
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?

Re: PHP Header and Footer

Posted: Mon Jun 01, 2009 4:47 pm
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;

Re: PHP Header and Footer

Posted: Tue Jun 02, 2009 3:24 am
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>
 

Re: PHP Header and Footer

Posted: Tue Jun 02, 2009 3:54 am
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");
 

Re: PHP Header and Footer

Posted: Tue Jun 02, 2009 9:51 am
by lmg
Thanks matt and Turv. That is EXACTLY what I was looking for!