creating a php captcha from scratch
<p>I need to add a simple captcha like word captcha or math captcha to the registration form. However the code is not working. Following is the code: (Please help me in fixing the code)</p>

<pre><code>&lt;?php
session_start();
//random string generator function
function random($length){
//create a or array string called chars
        $chars ="abcdefghijklmnopqrstuvwxyz23456789";
         $str = "";
        //is the variable which is equal to the length of the string called chars
        $size = strlen($chars);
        for($i=0; $i&lt;$length; $i++){
             $str .= $chars[rand(0, $size-1)];

    }
    return $str;
}
//the gd image adaptor in xampp
$cap = random(7);
$_SESSION['real'] = $cap;
$image = imagecreate(100, 20);
$background = imagecolorallocate($image, 0, 0, 0);
$foreground = imagecolorallocate($image, 255, 255, 255);
//this is going to write our string in the image
imagestring($image, 5,5,1,$cap,$foreground);
header("Content-type: image/jpeg");
imagejpeg($image);
?&gt;
</code></pre>

<p>registration page code</p>

<pre><code>   &lt;?php
//This function will display the registration form
function register_form(){

$date = date('D, M, Y');
echo "&lt;form action='?act=register' method='post'&gt;"
    ."Username: &lt;input type='text' name='username' size='30'&gt;&lt;br&gt;"
    ."Password: &lt;input type='password' name='password' size='30'&gt;&lt;br&gt;"
    ."Confirm your password: &lt;input type='password' name='password_conf' size='30'&gt;&lt;br&gt;"
    ."Email: &lt;input type='text' name='email' size='30'&gt;&lt;br&gt;"
    ."&lt;img src= 'captcha.php'&gt;  &lt;input type='text' name='captcha'&lt;br&gt;"
    ."&lt;input type='hidden' name='date' value='$date'&gt;"
    ."&lt;input type='submit' value='Register'&gt;"
    ."&lt;/form&gt;";

}

//This function will register users data
function register(){

//Connecting to database
$connect = mysql_connect("localhost", "root", "nokiae71");
if(!$connect){
die(mysql_error());
}

//Selecting database
$select_db = mysql_select_db("forumStructure", $connect);
if(!$select_db){
die(mysql_error());
}

//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$pass_conf = $_REQUEST['password_conf'];
$email = $_REQUEST['email'];
$date = $_REQUEST['date'];
$guess = $_POST['captcha'];

//Here we will check do we have all inputs filled

if(empty($username)){
die("Please enter your username!&lt;br&gt;");
}

if(empty($password)){
die("Please enter your password!&lt;br&gt;");
}

if(empty($pass_conf)){
die("Please confirm your password!&lt;br&gt;");
}

if(empty($email)){
die("Please enter your email!");
}


$real = (isset($_SESSION['real'])) ? $_SESSION['real'] : "";


if (empty($guess))
{ 
    die("Please enter the correct CAPTCHA.&lt;br&gt;");
} 
/*if ($real != $guess){
    die("things are bad");
}*/
if (!empty($guess) &amp;&amp; $guess !== $real)
{
    die("Please enter the correct CAPTCHA.&lt;br&gt;");
}

//Let's check if this username is already in use

$user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

//Now if email is already in use

$email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

//Now display errors

if($do_user_check &gt; 0){
die("Username is already in use!&lt;br&gt;");
}

if($do_email_check &gt; 0){
die("Email is already in use!");
}

//Now let's check does passwords match

if($password != $pass_conf){
die("Passwords don't match!");
}


//If everything is okay let's register this user

$insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
if(!$insert){
die("There's little problem: ".mysql_error());
}

echo $username.", you are now registered. Thank you!&lt;br&gt;&lt;a href=login.php&gt;Login&lt;/a&gt; | &lt;a href=index.php&gt;Index&lt;/a&gt;";

}
$act = isset($_GET['act']) ? $_GET['act'] : '';
switch($act){

default;
register_form();
break;

case "register";
register();
break;

}

?&gt; 
</code></pre>

<p>thanks I'm just a newbie in php and I would like to learn how to do this. I got the registration code on stackoverflow for learning purposes only... Your help or suggestions will be highly appreciated.</p>
