ShoeLace1291
ShoeLace1291

Reputation: 4698

Setting Page Titles When Using Global Headers in PHP

I was wondering how you guys set your page titles while you use global headers. I would like to be able to change my page title from page to page... for example, "Site Name : News Archives". Would the best way be to use JavaScript? If I did this with JS, would the new changes take effect in search engine results? Just wanted to get some input on this thought.

<?php

include('header.php');

switch($_GET['p']){

   case "news": 
      include('news.php');
   break;

   default:
       include('indexBody.php');

}

include('footer.php');

?>

Upvotes: 2

Views: 1569

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

If your looking to go down this road, perhaps try something like the following:

Store your variables in a data array and then pass them to your header/footer/content views before echo'ing out.

$data['title'] will be seen by your header and footer ect as $title

<?php
$page = (!empty($_GET['p'])?$_GET['p']:'index');

$data = array();
switch($page){
    case "news":
        $view = 'news.php';
        $data['title'] = 'Site Name : News Archives';
        break;

    default:
        $view = 'indexBody.php';
        $data['title'] = 'Site Name : Home';
        break;
}

echo load_view('header.php',$data);
echo load_view($view,$data);
echo load_view('footer.php',$data);

function load_view($path,$data) {
    if (file_exists($path) === false){
        return 'View not found: '.$path;
    }
    extract($data);
    ob_start();
    require($path);
    $out = ob_get_contents();
    ob_end_clean();
    return $out;
}
?>

Upvotes: 0

Klaus F.
Klaus F.

Reputation: 145

You can define a variable BEFORE including the header. This variable can be used then in header.php.

<?php

$pagetitle = "Site Name : News Archives";

include('header.php');

...

?>

Upvotes: 2

Related Questions