1

I've been stuck up on this topic for a while. As a project to help me learn MySQL, PHP, and database security a while back, I created a lightweight blog system. It blew up on me and a few of my fellow programmer buddies wanted to use it and now I am working on improving on its features.

The way it works, simply put, is that a blog post is uploaded, the title, author name, and date are stored in a database, but the actual content is used to dynamically generate an html page which is where the actual blog post is viewed. In the database, the path to each blog post is saved so that it can be used to access and view the blog post page. One of the features of my blog system is that the generated HTML is fully customizable, so you aren't limited by a specific template. The way I am currently doing that is by using a function that takes parameters of the values and then returns a HEREDOC with the format desired, which is then written to a file whose name is based on the post title.

function get_full_post_html($title, $author, $date_posted, $text) {
    return <<<EOT
    <!DOCTYPE html>
    <html>
    <head>
        <title>$title</title>
    </head>
    <body>
        <h1>$title</h1>
        <h3>$author</h3>
        <h4>$date_posted</h4>
        $text
    </body>
    </html>
EOT;
}

Not the cleanest solution, which is one of the reasons I am looking for a better way. The other reason why I need to rethink this, is because I am currently adding support for comments, which means the html page that is generated now needs to be dynamic, with my PHP method for getting the comments. Whereas before the blog posts were static content, now with comments enabled, the page becomes dynamic.

I had a look at this question, but frankly the answers were vague and didn't make much sense. The main question I am asking is, how are dynamic pages typically generated in PHP? For example, take this blog post on A List Apart: http://alistapart.com/article/tweaking-the-moral-ui. It has a physical page for the post, tweaking-the-moral-ui, but still has dynamic features like comments, ads, etc. How is this done?

Here is the link to this entire project on GitHub, if you are interested in understanding how it works in depth.

12
  • Although this is more primary opinion based, why you save HTML files since you are already using Database? Can't you just use a template system? You can always make your own template system ... and assign variables in that template and not vice-versa? You are extacting data from database and you are saving it as html in a bad manner, you should not ever save html pages as you at some point will need to change something and changing every time every html file is an unnecessary and avoidable job. Commented Dec 22, 2014 at 9:33
  • The main reason why I want individual pages is for SEO, so that when you search for something related to the post, it will hopefully show up in the results. Without individual pages, the post could never show up in search engines, right? Commented Dec 22, 2014 at 9:36
  • Wrong, please take a look at wordpress or any other blog system and you will never see html pages. You can always play with .htaccess and make scripts .html even if they are dynamic. Commented Dec 22, 2014 at 9:38
  • What you need is a well designed database.. Commented Dec 22, 2014 at 9:41
  • 1
    The search engine will see the post linked somewhere from your homepage or in pages linked in your homepage. This is first step of the proble, searchengine is not gonna read all files just because are there. For having pretty urls, or seo friendly, check my answer, is just an hint but should clarify you. Commented Dec 22, 2014 at 10:47

1 Answer 1

1

I think there is a starting mistake here.

You are writing pages to file to have a nice url instead of using a rewriteurl logic.

if you want to make them "dynamic" you can regenerate the file every time a user add a comment.

Or follow this answer of another question:

http://stackoverflow.com/questions/16388959/url-rewriting-with-php

So basically you add comment to your blog platform, and you regenerate the page once per request as many platform do. In your index.php you implement some logic to research from the url the post you need.

The pages will be visible because linked in some listing. In the listing instead of:

http://myblog.com/mypost.php?id=33

You will have

http://myblog.com/i love my dog/

Then in your index.php you will analyze

$_SERVER['REQUEST_URI']

and serve the correct post to the user or to the web crawler of search engine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.