How to add unique titles in php
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:
<title>
<?php
if($title){echo $title; }else{ echo "Generic Title";}
?>
</title>
Static Page
<?php
$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.

do the same also for keywords
If you use SEO friendly URLs, and are lazy like me, you could use these to set the title. This removes the need to specify anything on any page.
as in:
$title_holder = explode(“/”, $_SERVER['PHP_SELF']);
gets the URL i.e. domain.com/my-page-title and explodes it on the first ‘/’
$title = $title_holder[1];
sets $title to the bit *after* the forward slash
$title = str_replace(“-”,” “,$title);
Replaces hyphens with spaces
$title = ucwords($title);
Converts the first letter of each word to capitals so it looks neat
thanks , really helpful.
i searched it a lot and finally got it here.
proper working one.
i liked your other posts too.
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 ?
thanks for this great article.
making unique titles is what i was finding .
well i was facing problem and couldn’t really was able to solve it.
your article has somewhat give me the idea what was the problem i was facing.
thanks.
well, thank you , you don’t know how much this post helped me.
there maybe other solutions to this one , but your post makes it look easy.
Thanks its really good explain nice example given
what wrong am i doing , can’t get this code work
i was looking for something similar , i hope this would help.
The site is calling content from a database, and I am wondering how to have the Meta Title match the topic of the page. Currently every single page has the same title. I want each page to have a unique title. For example, let’s say the following page is about “cats”.
http://www.example.com/products.php?item=4
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']);
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.
Thanks for the great and easy tutorial . I need the ability to add a unique title tag and also a description to each page.
Hey, the code is really helpful. Thanks a ton. Easy and a nice way to add unique titles.
Helpful code, I was searching for it for a long time now. Thanks!
Thanks for postings this code.
I have saved a copy of it.
I am not really familiar with Php coding but
I’ve been keeping necessary codes for Web Development.
I am not good in Php Coding but I keep and save necessary codes
that could help me in the future.
So thanks for this Code I will keep a copy of this.
Thanks for this bit of code. does this mean I can put that into my site, then when I need to change the title or when I do a new site I dont need to enter a title for each page separately but by using that code every page will be using the title?
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.
Myself, i prefer to just use plain html instead of embedding it into php. It looks messy and sometimes i make mistakes