Coding - PHP permalinks for Blogger

Introduction

When your blogger archive files do not have the name that blogger thinks they have, you might find yourself unable to make permalinks.

My archive files to not have the same name as the <$BlogItemArchiveFileName$>, because I use the blogger-generated file as a PHP include.
As I archive my blog entries by month, keeping the archive index up-to-date by hand isn't a great pain, but I was disappointed to have to give up on permalinks.

I have come up with two PHP workarounds that I would like to share with you. Comments are welcome!

Simple but buggy method

The first method works fine as long as all the posts on the main blog page belong to the same archive page. Otherwise (and this is bound to happen) you get some broken links.

We'll assume that each page defines a variable called $filename which contains its page name (i.e, the last part of the url - the strtok() function can be used to do this.)
This is what the permalink will look like (in the blogger template):

<a href="<?
if ($filename==index.php)
{$link="http://".$_SERVER['HTTP_HOST'] . "/archive/currentarchivename.php";}
else {$link="http://".$_SERVER['HTTP_HOST'] . "$PHP_SELF";}
print($link);
?>#<$BlogItemNumber$>" title="Follow me for permanent linking.">permalink</a>

The urls above also assume that the main blog page is named "index.php" and that the archives sleep in the "/archive" directory.

The script first checks if we are on the main blog page (index.php).
If we are, the link is set to the current archive page. This part of the script needs to be ajusted manually when the archive page changes (quite a hassle if you have to do it every week!), and it is here that comes the bug: if you have posts belonging to different archive pages on the main blog, all your permalinks will point to the same archive page - hence broken links.
If we are not on the main blog page, we have to be in the archive - in which case links point to the page they are on.

More elegant solution

First, big thanks to daniel for getting this one to work - and for writing such pretty code.

This script is much more elegant than the last and actually works. The only thing is that it needs quite precise tailoring to your blogger settings. This example is designed to work with a <$BlogDateHeaderDate$> of the following format: Monday, November 27, 2000 (and will need to be modified depending on your formatting choices).

<?
function date2file($date) {
$months = array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
$date = strtolower($date);
$bits = explode(", ", $date);
$done = false;
for($i = 0; $i < count($months) && !$done; $i++) {
if(strstr($bits[1], $months[$i])) {
$month = $months[$i];
$done = true;
}
}
$filename = "log" . $bits[2] . $month . ".php";
return $filename;
}
?>

In the <BlogDateHeader>, instead of <$BlogDateHeaderDate$>, insert the following code:

<? $blogdate=("<$BlogDateHeaderDate$>"); $archivefile=date2file($blogdate); print($blogdate); ?>

Then, you need to call the function with the following line (adapt to your paths):

<a href="http://<? print($_SERVER['HTTP_HOST'] . "/archive/$archivefile"); ?>#<$BlogItemNumber$>" title="Permalink...">Permalink</a>

There! and most of all, don't forget to add the <a name="<$BlogItemNumber$>"></a> somewhere in your post!

::: discussion ::: (1)

This site will look much more pretty in a browser which supports web standards, but it is accessible to any browser or Internet device. Browsers which do not support CSS will get this message, even if they are otherwise standards-compliant. Do you want to upgrade?

French - français! Ce site aura l'air bien plus joli dans un navigateur conforme aux standards du web, mais il est accessible avec n'importe quel navigateur ou dispositif Internet. Désirez-vous faire une mise à jour?