Website-Redesign/content/posts/PHPguestbook.md

62 lines
4.6 KiB
Markdown
Raw Normal View History

2024-04-26 22:08:21 -05:00
---
title: "PHP Guestbook"
date: 2024-04-20T14: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).
# A guestbook
There was a lot of places I went to figure out this, 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](link).
This has been implemented elsewhere, but I wanted the codebase to be very easy to maintain, so I created my own, so I have 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.
## What this actually needed to do.
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. 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](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