Move everything from $md5 = ... to imagepng($captcha); to a new file called image_gen.php, or w/e you want to call it. Make sure you start a session on that page as well.
In your original document, make sure that the session is still started.
Wherever you want the captcha, put this:
<img src='image_gen.php' alt='Captcha Image' />
<?php
//image_gen.php
session_start();
// Make captcha
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,7);
$captcha = imagecreatefrompng("./img/captcha.PNG");
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
imagestring($captcha, 5, 20, 10, $string, $black);
$_SESSION["CAPTCHA_CHALLENGE"] = md5($string);
header("Content-type: image/png");
imagepng($captcha);
?>
<?php
// Original file
session_start();
$result = " ";
if($_GET["action"] == "proc"){
$response = $_POST["response"];
if($response != $_SESSION["CAPTCHA_CHALLENGE"])
$result = "The code you entered was incorrect.";
else $result = "Correct!";
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<body>
<div><?php echo $result; ?></div>
<div id="test">
<h1>This is a test<h1>
<form name="x" method="POST" action="Test.php?action=proc">Type the code on the right:
<input type="text" name="response" size="7" maxlength="7"/>
<img src="image_gen.php" alt='captcha image' /></form> </div> </body></html>
I apologize about the white space issue, my copy and paste is not working right.