Tag Archives: PHP

"cloud" notepad (PHP)

Whenever I am playing Steam games I like to take notes - unfortunately the only way I could access my notes made on steam was to either make documents on a service such as google docs or onedrive, or alternatively mail myself my notes. docs and onedrive have quite a bit of overhead I did not want - I just wanted something to quickly note down things such as "this link x.y.z could be interesting for project ยต".

Making notepad in itself was easy enough - but a friend of mine wanted access to this as well so my original approach fell short here. Originally, I had a .txt file on the server which would be modified on a simple HTML page using a bit of jquery and javascript. Now, I decided to not create a complete hack of a project (two .txt files) - but to actually use a database, and provide a login for him and me - and possibly other friends wanting to use notepad. The project, as it stands at the time of this writing, has to following basic features.

  • Login system
  • Register*
  • CRUD notes

*register: uses a token so only people with the given token have access to this page.

As this was the second time I opted for PHP for a project (server requirement), some of the things here might be blantantly against PHP convention dictates. For instance, I follow Java standards for variable names and have implemented the Front-controller in a way similar to that of a Java webservlet. Otherwise, it should be a pretty standard OOP project.

UML
(created using PhpStorm UML creator)

Most of this should be self-explanatory. The Servlet talks to a Facade to get access to "backend" components. The IDatabase insures a few operations the database can perform and the class "OnlineDatabase" is an implementation using MySQL. MySQLDatabase would probably have been a better name - but I do not foresee it switching to postgresql anytime soon due to, once again, server requirements. OfflineDatabase would have been the implementation with a .txt file on the servers HDD - but I did not take time to fully expand on this original 'hack' any further.

Note.php and User.php provide mapping between the database and the php code. I dubbed them "POPHPOs" in the comments - As calling them "POJOs" would have been one step too far, and part of the PHP community might already cringe at the sight of having a Servlet.

Although I believe most of this code is interchangeable between Java and PHP, or at least has a high degree of similarity, I will not put the whole code here. Just some notable things that might not be done in standard PHP that I want to highlight! Admittedly, I have no idea how the PHP community handles these things - and there might be more similarity between Java and PHP than I am aware of.

First of all, the notation of having a Servlet. Every call is made to a page called index.php, which calls the Servlet's processRequest.

<?php
require_once 'php/controller/Servlet.php';
$servlet = new Servlet();
$servlet->processRequest();
?>

Note that I did leave this out of the UML - as I do not consider this class to be interesting enough. (Just like a crypto class for the passwords in the facade).
In our Servlet - we check wether or not there is a post or get argument with the name "action".

A small piece of the code would be the following.

 public function processRequest()
    {
        session_start();
        $this->redirect = true;
        // echo 'processing';
        $action = "login";
        if (isset($_GET['action']))
        {
            $action = $_GET['action'];
        }
        elseif(isset($_POST['action']))
        {
            $action = $_POST['action'];
        }
       $nextPage = "";

        if ($action == "gotologin")
        {
            $nextPage = 'home.php';
            // try some database stuff
        } elseif($action == ...){..}...

I realize now that PHP might actually have support for switching on Strings (Scratch that, php HAS switching on strings). This kind of gives away that my previous projects have been constrained by old JDKs. The redirect flag is true for most cases in the pseudo-switch, but sometimes you just want to pass on data without having it redirect to another page. (Providing data with AJAX for example).

Posting the whole code here would become tedious - if you are interested in an explanation with the UML diagram and full code, feel free to mail me at (meeusdylan(at)hotmail(dot)com). Furthermore, I can not help feeling like the usage of global variables as a way to pass on data in PHP is a hack. Nevertheless - always choose the language which is best for the job, PHP was the best choice of the choices which were available to me. (Which were limited by server constraints, I frankly would have used Java I could have - because it would have been done in half the time this project took. Even though it just took about 2 days - the mistakes made due to a lack of understanding of PHP were wasted time. Though time you enjoy wasting is never really wasted time!

Design-wise it was a quick and dirty job: bootstrap and minimal jquery effects. Design is not my thing so spending time on this would be time wasted - especially with bootstrap.

Create note.
page2

Notelist.
webpage1

Notes.
thirdpage

The login and register are standard bootstrap login/register pages.

It was a fun project, quite a bit for nostalgic reasons as well. I got into programming so I could make my own tools that I needed - but have been preoccupied with making things for either business or university - not for myself because I needed it. Now, I can happy scratch down some notes while playing games - or on my smartphone - and access them on my desktop. Add on that the joy of working with a language you are not good in - and completing it - and you have the best possible way to spend your time.