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.

Community
  • 1
  • 1
samrap
  • 5,595
  • 5
  • 31
  • 56
  • 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. – Mihai Iorga Dec 22 '14 at 09: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? – samrap Dec 22 '14 at 09: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. – Mihai Iorga Dec 22 '14 at 09:38
  • What you need is a well designed database.. – Sampath Liyanage Dec 22 '14 at 09:41
  • Well that's what I am trying to figure out. An alternate way to what I am doing. I just don't know what that alternate way is. How, for example, does Wordpress save blog posts, and how are they visible to search engines? – samrap Dec 22 '14 at 09:43
  • Think like this: `[database] <-> [extract data] <-> [assign data to script] <-> [display template with variables]`, variables can be inside that template. – Mihai Iorga Dec 22 '14 at 09:43
  • They are visible throw meta tags, html tags ... like you use `

    ` ... etc, exactly like you think, but full html page should be as a static template and not in a function, every time the page is accessed your functions should assign variables in that template and display it. That's what PHP is for.

    – Mihai Iorga Dec 22 '14 at 09:45
  • That was my original thought, by using a query string with the post id or a GET request or something of that sort. But you're saying that if I just have one template and generate the data inside that template, search engines will see each post individually? That doesn't make sense – samrap Dec 22 '14 at 09:46
  • As I said, playing with `.htaccess` cand make miracles, `post.php?id=2` can become `your-post-title-2.html` or even without any `id` in address. – Mihai Iorga Dec 22 '14 at 09:48
  • And search engines would index each one as an individual entity? – samrap Dec 22 '14 at 09:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67451/discussion-between-mihai-iorga-and-samrap). – Mihai Iorga Dec 22 '14 at 09:49
  • 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. – AndreaBogazzi Dec 22 '14 at 10:47

1 Answers1

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

AndreaBogazzi
  • 14,323
  • 3
  • 38
  • 63