Manu Phatak

Manu Phatak » Enhancements

Enhancements

I have decided to log the enhancements and functionality added to the site.

Dynamic Sitemap Generator

November 19, 2008

In an effort to optimize search results for my website I want to submit a sitemap to Google. If I make an update on my site, updating the sitemap is typically the last thing on my mind.

The sitemap standard is to create an XML file but google accepts PHP files if the actual source looks like XML. Take a look here; it doesn't look like much--you have to check out the source code.

This script works by accessing the directory where my web files are found and by pulling all the files from the directory. In the script I dictate certain parameters--for example, the file extension must be PHP or HTML--and there are a few files I want to exclude--remember header.php and footer.php? After I have isolated the files that I want to work with I can pull the time--accurate to the second--that each was updated. To finish up I add the sitemap XML syntax and I tell google where to find my sitemap.

Source Files

I have 2 upgrades in mind: (1) I want the script to create an XML file and (2) I want to figure out how to pull variables or <meta> content from the pages without loading them (or using a database)--this would allow me to set the "Priority" and "Change Frequency" tags on the page directly.

<?php
$site = "http://www.mphatak.com/";
/* Doesn't $dir remind you of a database connection handle? */
$dir = opendir($_SERVER['DOCUMENT_ROOT'].'');
$pages = array();
$ignoredpages = array("header.php", "footer.php", "sidebar.php");

$filetypes = array(".html", ".php");

/* This function approves of files based on there file extension */
function extension($filename = false)
{
global $filetypes;
foreach ($filetypes as $type)
{
$extension = substr($filename,-strlen($type));
if (strtolower($extension) == strtolower($type))
{
return true;
}
}
return false;
}

/* If files meet all the criteria they are added to $pages */
for ($i = 0; ($file = readdir($dir)) != false ; $i++)
{
if (extension($file) == true && !in_array($file, $ignoredpages))
{
$pages[$i] = $file;
}
else
{
$i--;
}
}

/* Page Header */
echo
'<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" . "\n";

/* Page Body - List Pages with XML/sitemap syntax */
foreach ($pages as $page)
{
$loc = $site.$page;
$lastmod = date("Y-m-d\TH:i:sP", filemtime($page));
echo
"<url> \n" .
"<loc> $loc </loc> \n" .
"<lastmod> $lastmod </lastmod> \n" .
"</url> \n \n";
}

/* Page Close */
echo '</urlset>';

closedir($dir);
?>

HTML to PHP

November 19, 2008

I extracted the components of each page into 4 reoccuring sections: header, sidebar, content, and the almost non-existant footer. Each section now has its own php file and each page assembles the 4 sections to create the page. Essentially, I am isolating the content--it's a really primative content management system.

The advantage of this is found in the cascading effects of design and structural changes. What does this mean? I'm considering adding a real footer to my site. When I do that, I really don't want to have to copy and paste it to every page--potentially breaking an html tag. This system allows me make a few simple changes to the footer and it is automatically in effect on all pages.

Source Files

How this works:

header.php

This file contains all of the HTML opening statements, <head>, and <meta> tags.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php echo
"<meta name='description' content='{$description}' />" .
"<meta name='keywords' content='{$keywords}' />" .
"<title>Manu Phatak &raquo; $page </title>"
?>

<link href="style.css" rel="stylesheet" type="text/css" />

</head>


<body>

<!-- Opening Statements -->
<div id="container">
<div id="hd">
<h1><a href="index.html">Manu Phatak</a> </h1>
</div>
<!--hd-->
<div id="bd">

<div id="content">

sidebar.php

This is my navigation bar--just an unordered list.

</div>
<!--content-->

<div id="nav"> Pages
<ul>
<li><a href="index.php">&raquo; Home</a></li>
<li><a href="about-me.php">&raquo; About Me</a></li>
<li><a href="experience.php">&raquo; Experience</a></li>
<li><a href="contact-me.php">&raquo; Contact Me</a></li>
</ul>

</div>
<!--nav-->

footer.php

Even though I don't have a footer, I need this file to close html and div tags.

</div><!--bd-->
<div id="ft"></div>
</body>
</html>

Content Page -- ex. about-me.php

This is the content page. I use a php include statement to call the other 3 parts of the page.

<?php
$page = 'About Me';
$description = '10 things you need to know about Manu Phatak';
$keywords='Manu, Phatak, Manu Phatak';
include('header.php');
?>

<!--Where do these variables go?-->

<h1>10 things about me:</h1>
<ol>
<li>Content Goes Here</li>

</ol>

<?php
include('sidebar.php');
include('footer.php');
?>