website/content/posts/PHPguestbook.md

64 lines
5.3 KiB
Markdown

---
title: "PHP Guestbook"
date: 2024-04-27T14:24:47-05:00
tags:
- PHP
- learning
image:
draft: false
comments: 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](https://fbievan.live/redesign.html) (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](https://w3schools.com) (**JS WARNING + AD WARNING**), the [PHP Documentation](https://www.php.net/manual/), and [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/). 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](https://git.fbievan.live/fbievan/Website-Redesign/src/branch/main/guestbook).
A guestbook made in PHP has existed 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](https://alpha.polymaths.social/@amin/statuses/01HW9D7JDTZK4HH6SCB4JAS2DW) 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 set up a $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 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. The whole reason I started this project, is that I wanted to do it without JavaScript.
## 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 bit extra to work out the kinks with.
## Concluding thoughts
I found this endeavor 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 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](https://readable-css.freedomtowrite.org/) and his support on the fediverse.
* [Website](https://benjaminhollon.com/)
* [Fediverse](https://alpha.polymaths.social/@amin)
2) Sire for making me laugh
* [Fediverse](https://alpha.polymaths.social/@sire)
3) Tyil for teaching me a few things
* [Website](https://www.tyil.nl)
* [Fediverse](https://fedi.tyil.nl/@tyil)
4) R.L Dane for being around
* [Blog](https://rldane.space/)
* [Fediverse](https://fosstodon.org/@RL_Dane)
5) Orbital Martian for being around
* [Blog](https://orbitalmartian.codeberg.page/)
* [Fediverse](https://alpha.polymaths.social/@orbitalmartian)
6) Sotolf for being around
* [Fediverse 1](https://alpha.polymaths.social/@sotolf)
* [Fediverse 2](https://social.linux.pizza/@sotolf)
7) Marshmallow Kiwu for being around
* [Fediverse on kpop.social](https://kpop.social/@ShaMyouiMo)
## 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