PHP simple guestbook using captcha






PHP simple guestbook using captcha


PREREQUISITES:
Script below will need 'php-gd' to run.
If you are using CentOS or RedHat, simple perform: yum -y instal php-gd
There is also another condition in order to make it work: you need to set permissions to write.
For example: chown apache gbook.php; chmod 700 gbook.php

WHAT THIS IS ALL ABOUT:
Most of the Internet sites will show you how to create images on the fly from another php file
using this construct: <img src=create.php>
If this is not acceptable for you, and you need to run al from the single file, just look at the script below.
You will have to use 'php output buffering': ob_start()
And you will need to encode all the jibrish to base64.
Now, when you need randomly generated image just call it from your own function like: img();


<!DOCTYPE HTML>
<-- free for personal use -->
<-- donate $5 for commercial use use -->
<-- © by oTo -->
<head><title>GUESTBOOK</title></head>
<body style='background:#001;font-family:helvetica;font-size:10px;'>
<font color=white>
<H1>GUESTBOOK</H>
</font>
<style>
 hr        {color:#f00;background-color:#f00;height:1px;border:0;margin:1em 0;}
 #cmt_guest{background-color:#ddd;color:#ff;}
 #cmt_wrp  {background-size:100% auto;background-color:#999;width:100%;color:#fff;}
 #tbl_cmt  {border-collapse:collapse;width:46%;table-layout:fixed;font-size:10px;}
 #td_cmt   {width:90%;word-break:break-all;background-color:#999;}
 #input_cmt{font-size:10px;}
 h2        {color:#aaa;}
 td {width:100px;}
</style>


<?php
session_start();

$dtcode=date("Y-m-d-H-i-s");

function img(){
          $code = substr(str_shuffle("123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ987654321"), 0, 5);
          $im = imagecreatetruecolor(60, 25);
          $fg = imagecolorallocate($im, 150, 150, 200);
          $cl = imagecolorallocate($im, 100, 250, 250);
          imagefill($im, 0, 0, 0);
          imagestring($im, 5, 5, 5,  $code, $fg);
          imageline($im, rand(2,50), rand(2,50), rand(2,50), rand(2,50), $cl);
          imageline($im, rand(2,50), rand(2,50), rand(2,50), rand(2,50), $cl);
          ob_start();
            imagepng($im);
            $contents =  ob_get_contents();
          ob_end_clean();
          echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";
          imagedestroy($im);
          if ($_POST["captcha"] != "" ){ echo " retry! "; }
          $_SESSION["code"]=$code;
}

if((isset($_POST["name"]) == "comment$dtcode") &&
   (!empty($_POST["message"])) && (!empty($_POST["name"]) &&
   ( $_SESSION["code"] == $_POST["captcha"] ))){
    $cmtfl=getcwd()."/".basename(__FILE__);
    $name = $_POST['name'];
    $message = $_POST['message'];
    $message = preg_replace('/</', "&lt;", $message);
    @ $fp = fopen($cmtfl, 'a');
if (!$fp){
    echo "<font color=white>error, wrong file permissions, no file or wrong dir!: $cmtfl</font>";
    header("Refresh: 3; url=$page");
    exit;
}else{
    $dt = date('Y/m/d H:i:s');
    $outstr = "\n<br><table id=tbl_cmt><tr><td valign=top id=cmt_guest>" .$name.
              "</td><td id=td_cmt><font id=cmt_wrp>" .$dt.  "<br />" .$message.
              "</font></td></tr></table>\n";
    fwrite($fp, $outstr, strlen($outstr)); fclose($fp);
    echo "<font color=white>Success!</font>";
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Refresh: 3; url=$page");
}
}else{

//print_r($_POST);

echo "<hr>
      <H2>USERS COMMENTS:</H2>
       <form action=". basename(__FILE__) ." method='post' name=comment$dtcode>
        <table>
          <tr>
             <td id=input_cmt><font color=white>Name: </font></td>
             <td><input type='text' name='name'></td><td></td><td></td>
          </tr>
          <tr>
             <td id=input_cmt><font color=white>Message: </font></td>
             <td colspan=3><textarea name='message' style='width:490pt;height=50pt'></textarea></td>
          </tr>
          <tr>
             <td id=input_cmt><font color=white>Code from img:</font></td>
             <td><input name='captcha' type='text'></td><td align=left>";
             img();
echo "       </td><td></td>
          </tr>
          <tr><td id=input_cmt></td><td><input type='submit' value='Submit'></td></tr>
        </table>";

}
?>

<hr /> <!-- --------------------------------------------------------------------------- -->