website/content/posts/PHPguestbook.md

5.3 KiB

title date tags image draft comments
PHP Guestbook 2024-04-20T14:24:47-05:00
PHP
learning
false true

What is PHP?

PHP is a server-side scripting language. It can be used for a ton of things. The Project I started on stems from building a new website Redesign (broken link). I did not originally plan to finish the guestbook first, but I got interested into PHP, and programming in general. This is one of the few projects that I coded from the bottom up.

A guestbook

There was a lot of places I went to figure out PHP, mainly from w3schools (JS WARNING + AD WARNING), the PHP Documentation, and MDN Web Docs. My plan from the beginning was using PHP to store data from a HTML Form, to a JSON File. This implementation could be later extended to use SQL, or other mediums. This is the first part of a guestbook. If you want to see my code, you should be able to see it here code.

This has been implemented elsewhere, but I wanted the codebase to be very easy to maintain, so I created my own. This led to me having a better understanding of PHP. I have always kind of felt that I have always taken, without giving anything back, so I started work on this project, and hope that this can be a learning tool for some people.

This blost is meant to be most of a preliminary to a further article about the complete redesign of this site, however I figure that a PHP development article would be pretty cool, so here it is.

What this actually needed to do and the Original Structure of the Code

First, I needed to receive a request, this was done with an HTML form with a POST request. This POST was to a php script, which needed to first cleanse the HTML code of any scripting or styling, this i used htmlspecialchars function inside of PHP. I stored all of these in variables.

Secondly, I needed an array of these variables, so i setup an $savemsg array, which stored the name and message of that request in one array. After this, I used some IF statements to not exit from situations where the name or message was invalid. After those checks, the code also has already stored the existing data from the JSON file.

I then used an "array_unshift" to change the array that was gathered from the JSON file, and then with a file_put_contents then rewrote over the existing JSON file

A redesign

Well, I didn't like the structure. At a meeting at my schools coding club (hey guys if you see this). I heard about classes and those structures, and I thought, I can use that in my own code. So when I got home, I redesigned the code to use a class. This meant that my code was now treating the variables as a variable inside of an object. This allowed for much easier expansion of what I wanted to add on later. While at it I also added dates, while there is no way to get the timezone of a user (without some major hacks that I did not want to do), without JavaScript. Which was the whole reason I created this project. So I settled for simply storing the date.

RSS Feed

This is a very similar structure to what I used in HTML with PHP, to do this, I simply used a foreach loop which cycled through every object in the JSON array and make an item with the title of the item's user, and then the description being the message. It was fairly simple, but took a little bit extra to work out the kinks with.

Concluding thoughts

I found this endeavour to be very beneficial in terms of having something to code. The obvious next step would be to do the same thing with SQL. I believe this project is something I would recommend others doing. This is something that taught me a lot about the way that programming works. It had me thinking of solutions to these problems, and had me challenge myself in many different ways. I truly feel like I learned a ton through this process. There are no words that describe how much time I spent working on this project, and how much I appreciated anyone following me on the Fediverse. Shoutouts to

  1. Benjamin Hollon (For his work on readable.css and his support on the fediverse.

  2. Sire for making me laugh

  3. Tyil for teaching me a few things

  4. R.L Dane for being around

  5. Orbital Martian for being around

  6. Sotolf for being around

  7. Marshmallow Kiwu for being around

Release

The test is available at https://testing.fbievan.live/guestbook

The code is available at https://git.fbievan.live/fbievan/Website-Redesign/src/branch/main/guestbook