Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Create a blog from scratch with PHP and Subversion

You don't need fancy software to create a blog with a powerful undo feature

Tyler Anderson (tyleranderson5@yahoo.com), Freelance writer and developer, Backstop Media
Tyler's photo
Tyler Anderson has graduated with a degree in computer science in 2004 and a Master of Science degree in computer engineering in December, 2005, both from Brigham Young University. Tyler is currently a freelance writer and developer for Backstop Media.

Summary:  PHP is a great Web programming language to use when creating dynamic Web sites, such as blogs. This tutorial explains how to build a blog from scratch, while storing data using flat files. The Web site will be backed up on a remote system using Subversion, protecting data in the event of a Web-site crash. Subversion is growing in popularity, and it is a great candidate to back up the Web site. With Subversion, it's also possible to roll back the Web site to earlier versions of the blog if your server crashes, or if you just didn't like last week's rambling.

Date:  14 Feb 2006
Level:  Intermediate PDF:  A4 and Letter (442 KB | 33 pages)Get Adobe® Reader®

Activity:  19611 views
Comments:  

Designing the blog editor pages

A blog isn't a blog without your own content. Here's where you'll design the adding and editing portion of your blog application. You will also develop the application to allow you to add new blog entries and view them from the main section.

Add a new blog entry, or edit/delete a current entry

This page gives you two options: add a new blog entry, or edit/delete a current entry. Define the edit/index.php file, as shown in Listing 8.



Listing 8. Defining the edit/index.php file
                    
<?php
include('includes/functions.php');
define('DIRECTORY_LISTING', "../blogs");

require('header.html');

print "<a href='index.php?edit=EditIt'>Edit/delete current blog entries</a><br>";
print "<a href='index.php?addnew=Add New'>Add new blog entry for today</a><br>";

require('footer.html');
?>

Notice here that the DIRECTORY_LISTING constant is a little different because the blogs directory is in a new location in reference to the main welcome page of your blog. After the header.html and before the footer.html files are required()'d, there are two links that give you options to edit blog entries or add new content to your blog.

Next, you'll define the simple header and footer files referenced above.


The header and footer files

These two files have the same purpose as the header and footer files for the main welcome page of the blog. Define the header file as follows:

<html><head><title>My blog editor</title></head><body>
<h1>My blog editor</h1>
<a href="..">Main blog page</a><br><br>
                

This makes it so that there will always be a link to the main welcome page of your blog, as shown in the last line above.

Now define the footer file:

</body></html>

That completes the Web interfaces. See Figure 2 for sample browser output.


Figure 2. Editing the blog menu
Editing the blog menu

Now create the functionality to add new blog entries.


Processing add new blog entry requests

Now you'll capture from the URL when a request to add a new page has been sent to your PHP application. Continue defining the edit/index.php file, as shown in Listing 9.


Listing 9. Capturing the desired command from the URL
                    
...
if($_GET['addnew'] == 'Add New'){
    displayAddNewBlogForm();
}
else{
    print "<a href='index.php?edit=EditIt'>Edit/delete".
          " current blog entries</a><br>";
    print "<a href='index.php?addnew=Add New'>Add new blog".
          " entry for today</a><br>";
}
...

Now instead of the links displaying again, a form allowing you to enter your daily log will be displayed. You'll define this function and its accompanying form next.


The add new blog entries form

Since you'll probably want to create at least more than one blog an hour, you're going to have to index the blogs in the file system down to the minute. On top of that, you'll need to make sure you're not overwriting a previous blog entry. This function helps to accomplish all that. Define the displayAddNewBlogForm() function in edit/includes/functions.php, as shown in Listing 10.


Listing 10. Defining the displayAddNewBlogForm() function
                    
function displayAddNewBlogForm(){
    print "<a href='index.php'>Editor Home</a><br>";
    $month = date("M");
    $day = date("d");
    $year = date("Y");
    $hour = date("H");
    $minute = date("i");
    print "<h2>Add new blog entry for $month $day, $year ".
          "$hour:$minute</h2>";

    if(is_file(DIRECTORY_LISTING."/$month $year/$month $day $year ".
            "$hour $minute")){
        print "You've already created a blog entry in the last ".
              "minute. Try deleting or editing it instead.";
        return;
    }

    print "<form action='index.php' method='POST'>";
    print "<input type='hidden' name='blog' value='$month $year/".
          "$month $day $year $hour $minute'>";
    print "<input type='hidden' name='dir' value='$month $year'>";
    print "<textarea name='content' cols='100' ".
          "rows='15'></textarea><br>";
    print "<input type='submit' name='save' value='Save'> ";
    print "</form>";
}

First, create a link that allows you to return to the main editing page and cancel your current request to add a new blog entry. Then you get the current month, day, year, hour, and minute, which you'll use for the title information of the blog. You'll also check to make sure the blog doesn't already exist using the is_file() function. If the file doesn't exist, code execution will continue as planned and the form to add a new blog entry will be displayed. There are a few input items with type="hidden". These will allow you to propagate the directory, title, and name of the file you're going to add.

An example preview of the Add new blog entry form can be seen in Figure 3.


Figure 3. The Add new blog entry form
The Add new blog entry form

Next, you'll see the code that saves the new blog entry to the hard disk.


Saving new blog entries

You don't want your new blog to just vanish. Here's where you'll save the file so that visitors to your blog can see what you have to say.

Continue defining the edit/index.php file by adding file writing support, as shown in Listing 11.


Listing 11. Adding file writing support to edit/index.php
                    
...
require('header.html');

if($_POST['save'] == 'Save'){
    if(!is_dir(DIRECTORY_LISTING.'/'.$_POST['dir'])){
        mkdir(DIRECTORY_LISTING.'/'.$_POST['dir']);
    }
    $fp = fopen(DIRECTORY_LISTING.'/'.$_POST['blog'], 'w');
    fwrite($fp, $_POST['content']);
    fclose($fp); 
    print "Blog entry saved/updated<br><br>";
}

if($_GET['addnew'] == 'Add New'){
    displayAddNewBlogForm();
}
...

If the Save button gets pressed, the save> variable in the POST array will be set to Save. If this is the case, you need to save the content of the new blog entry. Also, for new months where a directory doesn't exist, you'll need to make sure that a directory exists for the month and year that you're saving a blog for and, if the directory doesn't exist, create it. Once you've ensured that the directory exists, create a new file, write the contents passed through in the POST request, and display a confirmation back to the user.

See Figure 4 for example browser output when a new blog entry has been added.


Figure 4. Saving a new blog entry
Saving a new blog entry

Next, you'll go back to the main welcome page and set up functionality for viewing the blog entries there.

4 of 11 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=406894
TutorialTitle=Create a blog from scratch with PHP and Subversion
publish-date=02142006
author1-email=tyleranderson5@yahoo.com
author1-email-cc=