Blog Post Page

Pre-Reqs

In this step, we'll create a single PHP page that can display any blog post (very dynamic!). Before we start on that page, we'll need to add some more code to our PageDataAccess class.

Updating the PageDataAccess class

  1. Open PageDataAccess.inc.php and add this method to the class.
  2. Open page-data-access-tests.php and add this code to it.
  3. Open the test page in the browser to make sure it's properly fetching a blog page. Note that the code also verifies that if there is an error in the SQL statement, the handleError() method is invoked. Also note that we have confirmed that the handleError() method is throwing an exception, just as it should do. We "caught" the exception with our test code.

Create a 404 page

Add another global function to the config file (it will redirect users to a 'page not found' page)

Create the Blog Post Page

  1. Inside the blog folder, create a page called blog-post.php
  2. Set up the page to look like this.
  3. Test out the blog post page by opening the browser and going to the blog home page, and then clicking on the link to the test blog post.
  4. Try playing around with the pageId url query string parameter to make sure the the page is properly redirecting to the 404 page when an invalid blog id is used.

Allowing Markup in the Page Content

Notice in the getPageById() method that we are not 'scrubbing' the content column when fetching a post from the database (by using htmlentities()). The reason for this is that we may want to allow SOME html tags to be included in the content.

Preventing XSS Atacks

Take a look at the getPageById() method that we created earlier and note that we are not using htmlentities() on the content column when we fetch it from the database. The reason for this, is that we want to allow you to add some HTML tags so that you structure/style the content on your blog posts. But this opens a huge security vulnerability in our code.

Now would be a good time for me to demonstrate a cross-site scripting attack (also known as XSS attacks) and then I'll show you how we can try to prevent them from happening. If a hacker finds a way to insert/update certain information into your database, he/she might be able to pull off an XSS attack. Right now, it may be difficult for hackers to get to the database, but we will soon be creating PHP pages that might give them some opportunities. So let's go ahead and deal with the problem now.

Follow Up Questions: