My Departement Of Nerdcore Enthusiasm

Tutorials and the like. Things which I have committed myself to. Often linux/UNIX-related.

fredag den 5. april 2013

Small, simple, simplistic and aesthetic, PHP and HTML guest book


This blog post presents a small guest book solution that you can implement on your own website. By guest book, I mean: An opportunity for people who visit the website to post comments and read others comments on the page. The solution consists of a single PHP/HTML page and a plain text file to store the written comments.

Screendump



I have long been fond of making bash shell scripts (Linux) on my computer and so I have a basic understanding of programming. I also tried to code in other languages, but here I have not yet achieved the same level of understanding and skill, therefore I am so proud and happy that I manage to get as much control over the PHP-codes and syntax that I could code this guest book for my website. A guest book that looked and acted like I wanted it. The solution I came up with I would like to share with you whom might be interested. For inspiration for making your own solution or to directly use my solution as it is on your own website.

As I said the guest book only uses one page. It has input facility and the view of previous posts on the same page. It is a completely open guest book, anyone can write in it without lock-inn or registration. Managing the guest book is done by editing the text file in which all messages are stored. This will typically say: Download the file, edit it and upload it again. (If you use Linux/UNIX, you can automate this process by creating a script where you use eg wput and wget to upload and download the file from the server).

I would like to explain the basic principle of the PHP code for this guest book. The trick is that there is an "if-statement" in the very beginning of the PHP-code that says

"If there is something in the variable "post" then add this content to a specified text file. If there is not then just load the specified text file and show it on the page."

When the page loads the first time, there is nothing in the post variable it is not until there is someone who has completed the form and press the submit button underneath that there will be anything ind the "post" variable. Therefore the script simply loading the specified text file and displays it on the page. Now when someone completes the form at press the submit button the page will be loaded one more time but now there is something in the "post variable" and therefore that content is written to the specified text file as stated in the if-statement in the beginning. Then the page is loaded one more time but now the post variable is empty again and therefor we go back to first case scenario where the guest book is simply loaded into the page.

I do not think I can explain the approach as simple and easy to understand as I would like, but I just do want to state that this approach in my opinion, is the guiding principle in the coding of this guest book. This is the principle which makes it possible to receive input from the user and display the guest book content at the same page. The rest of the PHP code is just about retrieving input from the user via an HTML form and adding this input to a specified text file.

If you want to use this guest book solution on you own website you can download the source code to it from here. Your server must support PHP for it to work but the vast majority of servers does that already. The source code is packed as a zip archive so you will have to find a way to unzip it before you can start to work with it.

PHP and HTML in interaction

If you want to use this guest book on your own website it will indeed be nice if it fully integrates into the design of your website. Therefore, I would like to review how you can put the PHP code of the guest book into the HTML code of your own HTML page.

So HTML and PHP are two different types of codes. A little simplistic, one can say that HTML can be used for presentation while PHP can be used for manipulation (actions.. changing things.. etc..). PHP makes it possible that what the user enters will be added to a specified text file where it can be stored and that the file is loaded into the HTML document. HTML makes the design and look of the page. PHP and HTML work very well together. You can put PHP code into the HTML document at (almost) any place in the HTML code. You use a PHP-tag when you want to insert PHP code in the HTML document. The tag looks like this:

<?php ... PHP code ... ?>

Pages which are primarily driven by PHP are often given file extension ".php" although they also contain HTML code. I like to start my PHP pages exactly as I start my HTML pages namely with a DOCTYPE definition and some initial HTML code. The PHP codes I insert further down in the document. I have made a small illustration of how I've sorted my PHP code in relation to my HTML in this solution.



There is a minimal version of the guest book with a minimal amount of HTML code. You can open that file (gbdemostrip.php) in a text editor and select and copy the two blocks of PHP code and the HTML form which is the code between the:

<form method="post" action="">
and the
</form>

The two tags included.

and paste them into your pages HTML-code in the appropriate places. After that, all you need to do is to upload the specified text file demoposts.txt and your own PHP/HTML page with the guest books PHP-code to your server then it should work.

The striped down version of the guest book you can download her as a zip fil (you will need to unzip it first)
You can download the archive here

I also made a small zip archive with a working demo of the guest book to upload and try out right away on your own server. The archive consists of four files. gbdemo.php, demoposts.txt, demobilled.png and englematch.css. If you upload the four files to your server and go to the file.

gbdemo.php

with your browser. It would look something like this:

http://www.myhomepage.org/gbdemo.php

you should have a working guest book solution on your own website.

You can download the archiv here

A small note about character encoding.

Character encoding is an interesting topic in it self that I do not want to go deeper into here. Here I will just say that the text in your documents no matter whether if it is pure texts or an HTML page or a java script can have different encodings. It is recommended that you use the encoding called UTF-8 because it is the most modern, international and flexible encoding. For some reason I could not get UTF-8 character encoding to work on my server. Therefore I have chosen to go with the older standard called iso-8859-1 so therefore all the elements used for my version of the guest book iso-8859-1 encoded. I just wanted to point this out. It is quite easy to change this if you want to when you have just found out how it works.

I wish you much joy and inspiration

Ingen kommentarer:

Send en kommentar