-->
Labs

Infinite monkeys coding my brain’s synapse ;-)

Hell’s Gourmet PHP Comments Script Canape

0
Filed under labs
Tagged as , , , , ,

 

Very well, this a pretty small comments script (as its Canape part of the name makes evident) and is my first actual PHP code editing and understanding project. It came out as a funny add-on to a small static page I published just as a joke for a friend of mine, a couple of days ago. I have to admit it was funny…to code it and to use it!

You can see here the page I was talking about, if intrigued, as an example of its functionality (MeLL need some paper bags :p).

The engine:

<?php
session_start();
if (isset($_POST['message'])) {
    if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {
        $message = htmlentities($_POST['message']);
        $message2 = htmlentities($_POST['message2']);
        $fp = fopen('messages.txt', 'a'); // This is the file where comments wil be stored. Should be writable.
        fwrite($fp, "<p><strong>".date("d-m-Y H:i:s")."</strong><br /><b>$message</b><br/>$message2</p>");
        fclose($fp);
    }
}
$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;
?>

We use a simple plain text file in stead of a database. Of course the file messages.txt must exist and be writeable by the webserver. This code MUST be placed above you HTML code. You can customize the way the comments will be displayed by editing the html tags that are hardcoded on the fwite function.