Wednesday, May 23, 2007

Processing Classic FTP Blogger pages as PHP



Applies to:
Working with:



As already mentioned, this applies to Blogger owners who publish their pages via FTP to their own website. If your pages are hosted at Blogspot.com for any flavor of Blogger, this will not work.

Why process Html pages as PHP?


PHP is a scripting language that will allow you to do many amazing things to your page that is not possible with standard Html. For example, you could modularize your pages into sections (header, footer, sidebar, etc.) and then instruct the server to include them into your page at the appropriate place.

The advantage of this is that you can make changes to your "localized include file" without having to republish your entire blog. This is known also as server-side includes.

Blogger variable capturing


Another practical use is that you can take a Blogger template variable and output it in a different fashion. Take the following line of Template code and see how Blogger outputs it:

This Blogger template variable:

<$BlogCommentAuthor$>

Gets published as:

<a href="http://webstractions.blogspot.com" rel="nofollow">Ronnie T. Dodger</a>


As you can see, the template variable is translated into a piece of Html code and then published via FTP to your blog page. You have no control over the formatting of the variable. It is, what it is.

With PHP we can capture that variable, perform a pre-written function on it and output it in a different way. Take this code snippet for example:

Our template code:

<?php remove_nofollow( "<$BlogCommentAuthor$>" ) ?>

Gets published as:

<?php remove_nofollow( "<a href="http://webstractions.blogspot.com"
rel="nofollow">Ronnie T. Dodger</a>" ) ?>


And our pre-written PHP function remove_nofollow outputs:

<a href="http://webstractions.blogspot.com" >Ronnie T. Dodger</a>



The basic premise here is that you revise the Blogger template with a line of PHP script that will capture the Blogger variable <$BlogCommentAuthor$>. Once published, the stage is set. When a user (or yourself) views the page, our pre-written PHP function remove_nofollow() will get called by the PHP interpreter and strip the rel="nofollow" from that line of code.

You will notice that our scripts are enclosed inside of <?php   ?> tags. This is what instructs your server to interpret everything between them as PHP script. More on that later. I just want to call your attention to it.

Server-side Includes


There are other things you can do with PHP. I mentioned earlier about server-side include files for your header, sidebar and footer. As an example, you can "cut" the code from your footer area to the end of the page in the template, "paste" it into a seperate file on your server and replace all of that inside your template with one line of code:

<?php include "footer.php"; ?>

Publishing with Blogger is wasteful. The way it is set up, we send the same chunks of code over and over again. By setting up server-side include files and using PHP to insert those chunks, we eliminate that waste. This will save on republishing time and bandwidth.

Another advantage is that you can edit the include file with your favorite software and have no need to republish your entire blog. Insert links into your blogroll, more footer information, maybe add some css coding, etc. As soon as you save your file -- the effects are live and for every page on your site.

Sounds kewl eh?

Processing .html pages as PHP


Your server will not process PHP script inside of web pages with the .html extension. Pages with a .php extension, however, instructs the server to pass the file through the PHP interpreter first. The interpreter acts on the script and returns the output back to the server for normal Html delivery.

The .htaccess File


Fortunately there is directive to instruct the server to process .html files as PHP script, via a file named .htaccess which will hold the instruction.

Copy the following code into a new Notepad file:

AddType application/x-httpd-php .html .htm

Save the file to your computer with the filename .htaccess (note that filename begins with a period). Upload that file via FTP into the directory where your Blogger main page is located. Your blog directory may be at the root domain or in a sub-directory named /blog or something else. You were the one that set it up, so you tell me.

This file will tell the server that from this directory, and every directory below it (sub-directories), whenever it encounters a file with the .html or .htm extension -- then pass it through the PHP interpreter first.

Testing your .htaccess installation


We are going to revise the template to include a short snippet of PHP script to test that everything is hunky-dorey.
Navigate to Dashboard > Template > Edit Html

Copy and Paste, exactly, the following code at the bottom of the template right before the </body> tag. I chose this place because some of you may have some wacky CSS that repositions your page over things (like Blogger navbars and such). Hopefully this part of the page is okay to see if the test works.

<?php echo "PHP is fun!!!"; ?>

Save your template and republish your Index only

If all went well, you should see the words PHP is fun!!! at the bottom of your page. If so, go back to your template and remove that line of code and republish your Index only.

If you do not see the words, drop me a comment and we will figure it out.

What do we do now?


In upcoming articles, we will be removing the NoFollow tag from comments, flipping Page Titles around (topic first, then blogname ... good for SEO), creating easier to edit blogroll blocks, blocks for labels (oh joy), and maybe a widget or two.

Stay tuned, more coming soon!!

3 comments :

MoonDanzer said...

Well I suppose I am the only nut at home trying to edit a template this week-end. But I made a really cool header. NP there...but I am trying to change my entire main BG to a dark blue. And I can only get either the top to go dark or the bottom...what code element am I missing to get the entire BG dark blue? TY..goin' nutz' here!

Anonymous said...

The main BG of the header or the body?

I take it that you are wanting to get rid of the dark blue graphic with rounded corners below the flag.

Edit Html: Find the line for #header-wrapper. Delete the contents between the curly brackets {}

MoonDanzer said...

TYVM for this info. I will work on it tom. SS...for getting back to you so late..I have been very busy. You are the best!