index.php

<?php
 /*header*/
header("Content-Type: image/png");

/*We'll set this variable later.*/
$new_string;

/*register the session variable. */
session_register('new_string');

/*You will need these two lines below.*/
echo "<html><head><title>Verification</title></head>";
echo "<body>";

/* set up image, the first number is the width and the second is the height*/
$im = imageCreate(200, 40); 

/*creates two variables to store color*/
$white = imageColorAllocate($im, 255, 255, 255);
$black = imageColorAllocate($im, 0, 0, 0);



/*Runs the string through the md5 function*/
$string = md5(rand(0,9999)); 

/*creates the new string. */
$new_string = substr($string, 17, 5);

 /*fill image with black*/
imageFill($im, 0, 0, $black);

 /*writes string */
imageString($im, 4, 96, 19, $new_string, $white);

/* output to browser*/
imagePNG($im, "verify.png");
imageDestroy($im); 

?>

formhandler.php

<?php

/*This starts the session that we created on the last page*/
session_start(); 


/*This trims the random variable of any white space that the user may have unknowingly put in.*/
$random = trim($random);

/*Below is a conditional statement: In English it reads, if the string that the user put into the text box is equal to what is in the image then print to the screen, "You are verified."  If they are not equal it tells the user to go back and try again.*/

/*We can use the variable $new_string because we registered it into our session on the last page, it retains its value that it had on the first page.*/
if ($new_string == $random){
echo "You are verified";
}
else{
echo "Please go back and get verified.";
}


/*I plugged our image in like I would any other image.*/
echo "<img src=\"verify.png\">";
echo "<br><br>";
echo "Type the code you see in the image in the box below. (case sensitive)";
echo " <form action=\"formhandler.php\" method=post>";
echo "<input name=\"random\" type=\"text\" value=\"\">";
echo "<input type=\"submit\">";
echo "</form>";
echo "</body>";
echo "</html>";

?>

why does it give error?

http://img412.imageshack.us/img412/9467/ivxb3.gif

this tutorial is in http://www.phpnoise.com/tutorials/1/1

if you deal with the session initialization before you send headers to the browser then you shouldn't have this error.

If you want to use the following:
/*header*/
header("Content-Type: image/png");

then you could store this in image.php and then call it like:

<img src="image.php" border="0" />

I hope that helps.

tim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.