Building a Weblog: Part 4 | WebReference

Building a Weblog: Part 4


[next]

Building a Weblog: Part 4

By Jono Bacon

Rolling Your Own Categories

Adding blog categories is the next part of the game. This page is similar to the page used to add comments.

First, create the form in a new file called addcat.php:

Add the usual lines of code at the start of the file, before any HTML:

With this page available to restricted users only, you need to check if the user is logged in. Do this by checking if one of the session variables exists; if it doesn't, redirect to another page:

NOTE

Never Assume

It might seem impossible to get to this page without clicking a link, and wouldn't that imply the user has already logged in? Although this sounds logical, someone could still type the URL directly. It's always better to explicitly check to ensure the user is logged in, rather than trust other pages to do that job for you.


Add the logic for when the user clicks the Submit button:

Within this code, an INSERT query is created and sent to the database. After the query is run, the browser redirects to the viewcat.php page to view the newly created category.

Finally, close the else and include the footer.php file (all after the form):


[next]

URL: