<?php
session_start();
$PHP_SELF = $_SERVER['PHP_SELF'];
$errore = 0;
if(isset($_POST['submit']) && isset($_SESSION['captcha'])) {
	if(isset($_POST['code'])){
		$_code = $_POST['code'];
		if($_SESSION['captcha'] == $_code){
			header("Location: http://richardhamel.com/mycaptcha/go.html");
			exit;
		} else {
			$errore = 1;
		}
	} else {
		$errore = 1;
	}
}
if(!isset($_SESSION['captcha'])){session_register('captcha');}	
$PHP_SELF = $_SERVER['PHP_SELF'];
$stringa = '';
$cifre = 5;
for($i=1;$i<=$cifre;$i++){
	$letteraOnumero = rand(1,2);
	if($letteraOnumero == 1){
		// lettera
		$lettere = 'ABEFHKMNRVWX';
		$x = rand(1,11);
		$lettera = substr($lettere,$x,1);
		$stringa .= $lettera;
	} else {
		$numero = rand(3,7);
		$stringa .= $numero;
	}
}
$_SESSION['captcha'] = $stringa;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Captcha entrance page</title>
<style media="all" type="text/css">
#captcha{margin:0 auto;text-align:center}
.errore{color:#f00}
.style2 {
	font-size: 14px;
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style3 {font-size: 14px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000099; }
</style>
</head><body>
<div id="captcha">
	<p><img src="captcha.php" />
</p>
  <p class="style3"><strong>To proceed to the web form please<br />
    insert the code you see above into the Code field below<br />
    and then click the enter button.
  </strong> <br />
    [ any letters you see should be inserted as CAPITALS and if you<br /> 
    have difficulty in reading the image above then refresh your browser <br />
    a few times until you see an image that is clear enough to copy
  ]  </p>
  <p class="style2">If you get the code wrong, you will be alerted and can try again </p>
  <form id="captchaform" action="<?php echo $PHP_SELF ?>" method="post">
		<p><label for="code">Code: </label><input type="text" name="code" id="code" /></p>
		<p><input type="submit" id="submit" name="submit" value="Enter" /></p>
  </form>
	<?php if($errore == 1){ ?>
  <p class="errore">The code you inserted was not correct .<br />
	<a href="<?php echo $PHP_SELF ?>">Click here to generate a new Captcha code and try again</a></p>
	<?php } ?>
</div>
</body></html>