You are invited to Log in or Register a free Frihost Account!

My conditional headers.

This is what I like to do with my headers. Makes for dynamic meta tags and title tags. I like to used this setup because it allows me to control all pages.

When I make my sites. I like to have them dynamic with header.php, content.php, footer.php... etc.

This would be part of my includes folder.




<meta name='Name' content="Description" />
<title>Some Title</title>

New Example:


<?php include('head.config.php');?


$pageTitle = $_SERVER['SCRIPT_NAME'];
$pageMeta = $_SERVER['SCRIPT_NAME'];

//title tags

if($pageTitle == '/indexpage.php'){
echo "<title>Some Title</title>";
}elseif($pageTitle == '/aboutpage.php'){
echo "<title>About Some Title</title>";
}else {


//meta tags

if($pageMeta == '/indexpage.php'){
echo "<meta name='Name' content="Description" />";
echo "<meta name='Keyword' content="Keyword, Keyword, Keyword" />";
}elseif($pageTitle == '/aboutpage.php'){
echo "<meta name='Name' content="Description" />";
echo "<meta name='Keyword' content="Keyword, Keyword, Keyword" />";
}else {


I am not sure what is your question or this script is just explanation how you are make dynamic title and meta tags. Never mind, for me is better option to use case/switch function or MySql instead of if/else function. If you have site with large number of pages you need lot of if/else statements what will slow down your site.

Fire Boar
Better is to simply set the data in the file itself. For example, in your template file you might have something like...

<?php $meta->setTitle("The page title") ?>

<p>The content specific to this page goes here.</p>

The advantage of this is that it keeps all information about a particular page together. With the other approach, you have one file whose size depends on the number of pages you have, and this file needs to be included on every page. This is pretty bad for scalability.
This is not a question. I was just trying to share some idea's of how im using simple php to make editing site's better.

Idea behind moving title and meta tags to a single page is to make editing easier.. All I have to do is edit one page instead of every page. I do this with navigation,footer, and other blocks of code that is used throughout the site. I like the idea of case/switch function.. I think I will look into that. I have a site right now that is 42 pages so that option could take my idea to the next step.

As far as scalability, this concept would make adding & deleting pages very easy I believe. Plus I think that the php engine on the server would be taking much of the work load to display all the content, rather then the browser. I not 100% sure but thats what I think.

Either way. thanks for the feed back.
Fire Boar
Ah, I don't think you quite get what I mean. Common headers, footers, menus (perhaps with variations), whatever... these are very definitely must-haves in any decent website. My approach simply takes out what is not common, what makes each page different.

Suppose you have a file like this:

  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <?php echo $meta ?>
  <link rel="stylesheet" type="text/css" href="my-stylesheet.css" />
  <div>Common body stuff here.</div>
  <?php echo $content ?>
  <div>Perhaps some other common body stuff here too, some footer maybe.</div>

Here $content is all the content that is quite distinct between pages. This would likely be processed from a bunch of other PHP files, or it could be very simple, say a single static page. $meta would be an object that might look something like this:

class Meta
  private $title = 'Default title';
  private $keywords = array('default', 'keywords');
  private $description = 'Default description';

  function __toString()
    $t = "<title>$title</title>";
    $k = '<meta name="keywords" content="' . implode(' ', $keywords) . '" />';
    $d = '<meta name="description" content="' . $description . '" />';
    return "$t\n$k\n$d\n";

  function setTitle($v) { $this->title = $v; }
  function addKeyword($v) { $this->keywords[] = $v; }
  function setDescription($v) { $this->description = $v; }

With this, you can benefit from having some default values which you can change all in one place. You still control exactly how each part is displayed overall, just change the __toString() method. For instance, suppose you want " :: My Site" to appear at the end of the page title on every page. Just add this in the <title> tag in __toString(). Now if you echo/print an instance of the Meta class, it will do all the head stuff as if by magic.

Here's the clever bit. What you need to do now is make the same instance available when running the code that generates $content. You can do this in several different ways, some of which allow you to create single files like my example above which handle all the page-specific stuff and only the page-specific stuff. Which is the ideal scenario, because it means that the server is including less unnecessary code per page request.

tl;dr: having one huge if/else or switch/case somewhere is not ideal.
Related topics
HELP: setcookie() and header problems
questions :P
GD-library header probs
phpbb problem....
How many forum members...
Simple or Bulky/Heavy ?
PHP script problem
Some Big Web Headers
*Super Duper Urgent* "Headers Already Sent"
Stopping Apache from Sending Headers
Designing Logos / Headers / Signatures / Avatars - for Coins
php link
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.