≡ Menu

How to add unique titles in php for SEO

One of the thing I run into the most when evaluating a website for seo is the same title on every page. This can be fixed with a little bit of code. If all the pages are static you just go to each page. If the page is using a header file then you have to make some changes. If they have a lot of static pages with an include file for the header you are still going to have to go and change every page. If you have a lot of data in a database you can just make some changes in the header file. Here is the php code to fix the problem.

Website using header file on a bunch of static pages.

Include File:
<br /> <?php<br /> if($title){echo $title; }else{ echo "Generic Title";}<br /> ?><br />

Static Page
$title = "Unique Title";
include 'header.inc';
?>

Your still going to have to go to every page and put that on it. This allows you to keep a header file that you can change in one place. If your site is completely dynamic your going to need to add a title field to your database and populate it with Titles. Then use the same if statement on the title tag but echo the title from the db.

{ 9 comments… add one }

  • hyip March 5, 2009, 10:18 am

    do the same also for keywords

  • 99.9% Uptime Hosting April 21, 2009, 12:42 am

    I have just about completed my first php website and am not sure how to add a title tag for each of the pages ? I have used Title tag but the title is not coming up at the top of any of my pages.

    Is there another way title tags should be written when developing a site in php as to opposed from the traditional XHTML sites ?

  • HenryMorgan June 14, 2009, 12:28 am

    what wrong am i doing , can’t get this code work

  • derik June 14, 2009, 7:10 am

    i was looking for something similar , i hope this would help.

  • ogletree June 17, 2009, 12:07 pm

    Instead of

    $title = “Unique Title”;

    do

    $result=mysql_query(“select textfiled from record where user_index=1″);
    $row=mysql_fetch_array($result);
    $title = stripslashes($row['textfiled']);

  • ogletree June 17, 2009, 12:09 pm

    The code in the post is to be on two pages. You have a header file and a your page file. If your not using a header there is no reason to use this code. The post was to help people that are using a header and want unique titles.

  • photos on canvas July 8, 2009, 6:02 am

    Hi Ogletree, does it work with template CMS based website as I have seen such issues many times and it is really hectic work to optimize title tag when you don’t find any way to customize them. I guess your solution will work on the CMS template based sites as well.

  • Joe Barry August 31, 2009, 2:41 pm

    Myself, i prefer to just use plain html instead of embedding it into php. It looks messy and sometimes i make mistakes :(

  • ogletree January 12, 2011, 1:42 am

    Marci I know your spamming my site but that comment is just way too funny. I took out the links but left it in.

Leave a Comment