Includes and seo

Includes seem great but what is their effect to optimising pages…?

Meta tags and title tags are quite important for seo but if using includes how could you optimise each page individually…?

Is it a case of declaring variables in say, a header include, then rewriting their values relevant to each page…?

I have a page header which I include on every page.

I then have some switch statements that select the metadata title etc. to go with each page.

The page name is found from the URL.

When using a MVC(P) framework most of those things that appear in the header can be set at any point within the request before the HTML is output to page. That said most meta tags are worthless. I’m far from a SEO snake, but that is what I have been told/heard.

Meta keywords are worthless. Meta Descriptions are useful. Titles are definitely useful.

I use a function or method for the header rather than a static include for this reason. I can pass in this data which is supposed to be page specific.

Thanks guys

I agree meta keywords are pretty much outlawed due to spam.

I’m sure your ways are alot better. The switch statement might be something I could write.
The function or method for the header sounds very interesting but to be honest I would have no idea what a method is…

Could you perhaps post some example code to assist me in writing my own…?

A method is just a function of a class.
So you could have a page template class and have methods for header and footer.

I also define paths to CSS/JS files. That way a given page can request the files it needs and avoid having unnecessary scripts loaded on every page. When those files change I can also update the file name (to force a fresh download and avoid cache issues) and it’ll work across the site.

Basic example


<?php
class PageElements
{

	const site_name = 'My website';
	protected $title;
	
	public function __construct($title) {
		//append site name to title if not already in it
		if(false === strpos(strtolower($title), strtolower(self::site_name))) {
			$title .= ' - ' . self::site_name;
		}
		
		$this->title = htmlentities($title, ENT_QUOTES, 'UTF-8');
	}
	
	public function header($title, $description = null) {
	
		$head = "<!DOCTYPE html>
		<head>
		<meta charset='utf-8'>
		<title>$this->title</title>\
";
		
		if($description) {
			$head.= "<meta name='description' content='" . htmlentities($description, ENT_QUOTES, 'UTF-8') . "'>\
";
		}
		
		return $head . "</head>\
";
	}
	
	
	public function footer() {
		$year = date('Y');
		return "<footer>&copy; $year " . self::site_name . "</footer>\
";
	}
}
?>


<?php
$page = new PageElements('Lemon Scented widgets');
echo $page -> header('Our widgets have been dipped in quality lemon rind for that sumptous nostril flavour');
?>
<body>
<h1>Lemon Widgets</h1>

<?php
echo $page -> footer();
?>
</body>
</html>

The search engine crawler cannot see the PHP code or anything else that parses server side. Includes are parsed server side and delivered to the outside world. There is no way for the spider to know whether the file was dynamically generated or not.

Thanks Cranial that code is really helpful. Bit above me at the moment but Im getting the idea.

So how do you change the meta tag, titles etc for each page and make them seo friendly-spider readable…?

Lol now Im so confused

You echo the variable to display the data:


<?php
// Get the name of the page for the switch statement
$path_parts = pathinfo($_SERVER['PHP_SELF']);
$filename = basename($path_parts['basename']);

// Description and keywords meta tags
switch ($filename) {
case 'index.php':
$title = 'Index page title;
$metaDesc = 'Description';
$metaKey = 'Keywords';
break;
case 'cont.php':
$title = 'Contact';
$metaDesc = 'Description';
$metaKey = 'Keywords';
break;
etc.
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title><?php echo $title; ?></title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
	<meta name="description" content="<?= $metaDesc; ?>" >
	<meta name="keywords" content="<?= $metaKey; ?>" >
	<meta name="robots" content="index, follow" >
	<meta name="author" content="Rubblewebs">
	<link type="text/css" rel="stylesheet" href="layout.css" >
	<link rel="shortcut icon" type="image/x-icon" href="graphics/favicon.ico">

Thanks for your reply and code Rubble.

This is gonna sound really amateur, the switch codes you have, one for index and for one contact do you add each case to each page individually or keep them in an include and call them…?

I can see you echo title and the meta tags in the HTML but how does the page know which case to use…?

If Im confusing anyone I apologise

The page name is obtained from the URL using:


// Get the name of the page for the switch statement
$path_parts = pathinfo($_SERVER['PHP_SELF']);
$filename = basename($path_parts['basename']);

The current page name is now in $filename. That is used to select the correct section of switch and you add as many switch statements as you have pages.

You can add a echo $filename to see what it contains before the switch comand to confirm you are doing it correctly and remove it when the code is working OK.

Thanks for all your code and trying to explain it but I am still really confused as to how it chooses which switch…?

With your help I have learnt something new, basename, but in the code I am finding it still a bit difficult I think.

Am I right in thnking it activates which case to use due to the page being looked at e.g ‘cont.php’ will activate the code below it…?

Once again thanks dude

Yes the first part of the code gets the page name from the URL e.g ‘cont.php’

The switch comand then looks for the case ‘cont.php’ section.

The variables $title etc. are then populated with contents that relate to the cont.php page

The contents of the variables are then displayed in the correct place on the page e.g. <title><?php echo $title; ?></title>

NOTE: <?= $metaDesc; ?> is the same as <?php echo $metaDesc; ?>

Thanks so much Rubble that has really cleared things up for me and at last I get it lol. Thanks again to cranial for all the code aswell.

Im gonna implement this into the building of my blog aswell as eveything else everyone has helped me with so far.

Cheers guys for all your help :slight_smile:

Also, rather than having a Switch statement with ‘x’ number of metatag descriptions on it which would be lots of lines of un-needed code, why not (if you have it) save the titles, desciptions, keywords data in MySQL and dynamically call each pages data out depending on what page you are on.

That does sound really productive, I was just wondering does that work ok with how the spiders crawl and how Google indexes, the same point Michael Morris wrote about a few posts back…?

I don’t like the idea of having all the meta data for your whole site in one file, especially when tied to the base file name. What if one script can serve variable content? I’d rather pass the data in from each page, or if it’s DB driven content store it there and pass to the page template.

As for what how search engines index-- They don’t see your PHP. They don’t know how you structure any of this. That is SERVER side code. They see your URLs, and the HTML your script outputs. If you want to know what they see, view the source of the page in your browser.

ahh ok so the php will output the relevant HTML therefore it can be indexed and swept through by the spiders.

When you say from each page, do you mean write the php on each page or call it from an include…? As you can see Im still very far behind from figuring out your code lol

Calling it from the database sounds like a good idea, is that as reliable though…?

Well in my first post the class{} file would get included, because it’s common across all pages. It’d make no sense to copy and paste it everywhere.

BUT the title, and meta description is unique to each page, so you’d define it there, and pass it in to that class to vary the output for each page.

Are you familiar with functions and passing arguments?


function get_title($title) {
   return $title . ' - website name';
}

echo "<title>" . get_title('Why ducks are wet') . "</title>";

To a certain extent but very basic. Thanks for your patience by the way.

If you were defining the title and meta tags for each page (writing them seperately on each relevant page), what would you be varying…?