samrap
samrap

Reputation: 5673

Generating dynamic HTML pages with PHP

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.

Upvotes: 1

Views: 2200

Answers (1)

AndreaBogazzi
AndreaBogazzi

Reputation: 14741

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

Upvotes: 1

Related Questions