Документация
HTML CSS PHP PERL другое

Suggestions/Exercises

 
Previous
Table of Contents
Next

Suggestions/Exercises

Like the other major samples we have written in this book, space and instructional constraints mean that we cannot implement absolutely everything we would imagine a good blogging system should have.

A truly wonderful way to see how code works and learn how PHP works is to try to write some improvements to the system on your own. To help encourage this, we have come up with a small list of exercises, or projects, that can be implemented for this sample to improve it and from which you can learn more about its workings.

Improve User Listing on Home Page

The index.php page that we have implemented is extremely simple, and lists users in a table with a single column, which is a waste of screen space. You could improve this system in a number of ways:

  • Split the display into multiple columns. This is a bit more tricky than you might think, because you would ideally like to see

    Albert

    DeltaDeltaDelta

    BananaMan

    Everman

    Candy

    Frankie


    rather than

    Albert

    BananaMan

    Candy

    DeltaDeltaDelta

    Everman

    Frankie


  • As the number of users of the system increases, you would probably like to create a way to split the users into groups, perhaps by first letter of the username, journal activity, or some other criteria.

Allow Anonymous Comments

Our system currently requires users to be logged in as a valid user to write a comment in somebody else's journal. Many popular blogging systems in use on the Internet today permit anonymous comments, provided the journal owner allows it. Modifying the system to permit these anonymous comments should not be difficult. Just presenting users with a form asking whether they want to continue anonymously or letting them log in is a simple way to start.

Of course, with anonymous commenting comes a greater chance for abuse of the system, so journal owners should be allowed to block these anonymous comments if they so choose. You can add a new field to the Users table to reflect this preference.

Hierarchical Comments

All comments written in the current SimpleBlog system are flat .They are all basically replies to the entry with which they are associated. This does not give users a chance to explicitly reply to others' comments with their own comment. An improvement is to add an in_reply_to field to the JournalComments table (giving a value of -1 if the comment is simply a reply to the entry) and indent comments that are replies to other comments. Figure 32-7 shows an example of how this might work.

Figure 32-7. Hierarchical commenting.


Have User Creation Use Transactions

As mentioned in "User Management," we can have a bit of a race condition when creating new accounts. To prevent duplicate accounts, we use the UNIQUE keyword available in SQL and our database servers, but this can still occasionally result in an unfortunate error. Far more optimal is using a transaction to ensure that, during the period between one process checking whether a username exists and the insertion of that new user, no other process can perform either of these actions.

Implement a New strip_tags Function

The strip_tags function is not multiple-byte enabled, and it has a few other limitations. As an exercise in string manipulation, you could write your own version of this function that removes tags enclosed in < and > properly. It would be very important to test this on a wide variety of input values to ensure that things such as the following

Text <markup attr='>>>'> Text2

resulted correctly, as follows:

Text  Text2


Previous
Table of Contents
Next
© 2000- NIV