09 April, 2013

PHP dynamic title

It is useful when you optimize you site for SEO, this tutorial will show you how to keep in one single page which you will include in all others, the dynamic titles script for different pages. We will need to make a variable to get the page, and put an switch & case feature to estabilish the title, then we’ll print the resulted title.


<?php
$page = $_SERVER['PHP_SELF'];
if(isset($page)) {
   switch($page) {
     case "index.php":
$title = "this is homepage";
     break;
     case "products":
     $title = "products";
     break;
     case "contact":
     $title = "contact";
     break;
   }
}else{
$title = "default title";
}

print "<title>$title</title>";
?>

Copy and paste it on your page header.

No comments:

Post a Comment