Title tags in body (php)

11,419

Solution 1

I currently have the title tags in the footer of my site.

That is not valid. Title tags must be in the <head> section:

Every HTML document must have a TITLE element in the HEAD section.

Also, it will probably give real-world problems. I have never seen title tags used in the page's footer, and I expect they are going to be ignored by search engines there.

You should change the underlying architecture of your PHP script so you can know the title when the page starts.

Reference:

Solution 2

"if I place it in the head then it's blank, as the title content doesn't get loaded until the body." - that means that your site design is wrong

You should use templates and proper page layout.
In fact, your page code should output nothing, but only gater required data and then either throw an error or call a template

A concise example:

page called links.php:

<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>

Now template.php which is your main site template, consists of your header and footer:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>

and links.php is the actual page template:

<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>

easy, clean and maintainable.

Share:
11,419
Brian
Author by

Brian

Updated on June 04, 2022

Comments

  • Brian
    Brian almost 2 years

    I currently have the title tags in the footer of my site. This is because the title is dynamic, and if I place it in the head then it's blank, as the title content doesn't get loaded until the body.

    My question is, does it matter, SEO wise, that the title tags are in the footer as opposed to the header?

  • Brian
    Brian about 13 years
    What do you mean not valid? It seems to do the job ie. showing the title like I want it to. As well as showing the title on forums when copying the link. Though I did suspect it wouldn't work very well with SEO.
  • Gordon
    Gordon about 13 years
    @Michael HTML has rules. If you dont follow these rules, you are not writing valid HTML. It doesnt matter that browsers render it nonetheless. The title does not belong in the footer.