Individual page titles from same template file?

Hey everyone,

I am wondering how I can use PHP so I can have particular webpages have a unqiue (relating to the content on the page) html title.

My website is 100% PHP, with a very basic script calling for a header, ‘midde’, and a footer.php file with the main page content being loaded from content.php

The other pages contents are other php files on the server being called to load into the index.php file. So the links are like index.php?action=whatever

Index code:

<?php

    @include("header.html");
    @include("middle.html");

        if ($_GET["action"]) {
            $includeFile = $_GET["action"] .".php";
                if (@file_exists($includeFile))
                    @include($includeFile);
                else
                    echo "File does not exist!";
        }
        else {
            @include("content.txt");
        }

    @include("footer.html");

?>

I am wanting to add something to it, or rather to the header.html file (will change to php) to have it call in a defined page title for each particular page of my site.

My php coding experience is very limited and I don’t know if I am describing this clearly enough, but hopefully you can understand what I asking.


$titles = array(
    'home' => 'Home Page',
    'about' => 'About Page'
);

echo '<title>'.$titles[$_GET['action']].'</title>';

Thanks, that works well but how would can I maintain a page title for the index file?

Current code:

<?php

$titles = array(
    'pomegranates-and-your-health' => 'Pomegranates and Your Health / Pomegranate Nutrition -- PomsToYou.com',
    'cooking-with-pomegranates' => 'Pomegranate Recipes / Cooking with Pomegranates -- PomsToYou.com',
    'about-us' => 'About Us -- PomsToYou.com',
    'contact-us' => 'Contact Us -- PomsToYou.com'
);

echo '<title>'.$titles[$_GET['action']].'</title>'; 


    @include("header.html");
    @include("middle.html");

        if ($_GET["action"]) {
            $includeFile = $_GET["action"] .".php";
                if (@file_exists($includeFile))
                    @include($includeFile);
                else
                    echo "File does not exist!";
        }
        else {
            @include("content.txt");
        }

    @include("footer.html");

?>

My PHP is a little rusty… well, really bad I guess is a better way to put it :wink:

Are you wanting to have the titles stored in the database instead?

It’s quite strange to put all page titles into index file.
It must be stored somewhere else, with page content probably.

There are some pretty big assumtions going on in your code that may come back to bite you in the ***, here’s a better approach.


<?php
#define your pages here
$pages = array(
    'home'  => array(
        'title'     => 'Home Page',
        'header'    => 'header.inc',
        'content'   => 'home.inc',
        'footer'    => 'footer.inc',
    ),
    'about' => array(
        'title'     => 'About Us',
        'header'    => 'header.inc',
        'content'   => 'about.inc',
        'footer'    => 'footer.inc',
    ),
);

#if page exists,otherwise set 'home' as default
$page = false === empty($_GET['page']) && true === array_key_exists($_GET['page'], $pages) ? 'home' : $_GET['page'] ;

require($pages[$page]['header']);
require($pages[$page]['content']);
require($pages[$page]['footer']);
?>

Off Topic:

Edit: tips hat to pwm57 :wink:

Better what I would do is: just query the texts from the database and put the title of the text from the db to the title of the page, If it is home then check and put some default text in the title.

Anthony’s code is quite academic example, and some far away from the real life, in my opinion.
Page title belongs to the data. And shouldn’t be hardcoded in the code. And custom headers/footers are redundant.

But having an array with filenames is a good approach to avoid security risk planted in the original code. But not the best. If we set include folder and check input parameters against it, everything would be safe without job of maintaining an array.

I’d suggest OP to change whole paradigm.
There is one simple rule: Not a single character must be printed out until all page data gathered and ready.
So, if original pages are static, and contain html only, it’s easy. Do not include them but read into a variable.
If they are dynamic, each page must be divided into two parts: data gathering code and a page template. An index page would look like this (simplified):

<?
include 'page.php';
templater();
?>

where templater() function take all header, footer and page template together, fill it with page data and then output it.