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.
Now we show the comments:
<?php
readfile('messages.txt');
?>
This code you must place wherever you want the comments to be shown.
You can customize the way the form looks like by editing the html tags of its components, of course.
And that’s it! Of course we make no validation to avoid empty comments or spam. The idea of thsi code is not to be used on very hig traffic websites…it’s just as simple as it can be. Even the licensing notice has more lines than the actual code!!!
The complete source can be downloaded here (right click > save link as).
Hell’s Gourmet PHP Comments Script Canape
Tagged as comments, Hell's Gourmet, Hell's Gourmet PHP Comments Script Canape, php, script, web
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:
<?phpsession_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.
Now we show the comments:
<?phpreadfile('messages.txt');
?>
This code you must place wherever you want the comments to be shown.
Finally, the comments form is placed:
<form method="POST"><input type="hidden" name="token" value="<?php echo $token;?>" />
<P><b><font color="#FF0000">Tu nombre/nick:</font></b><BR><input type="text" name="message"><br /></P>
<P><b><font color="#FF0000">Tu comentario:</font></b><BR><textarea name="message2" cols="25" rows="8"></textarea><br /></P>
<input type="submit" value="Postea un comentario!">
</form>
Put wherever it pleases you the most
You can customize the way the form looks like by editing the html tags of its components, of course.
And that’s it! Of course we make no validation to avoid empty comments or spam. The idea of thsi code is not to be used on very hig traffic websites…it’s just as simple as it can be. Even the licensing notice has more lines than the actual code!!!
The complete source can be downloaded here (right click > save link as).
Hope it be as usefull to you as to me!
Si te gustó esta nota, podés invitarme una cerveza en agradecimiento. Y algún día quizá pueda yo invitarte una :D