验证码不起作用

I have designed the comments box for getting comments. Instead of using captcha plugins, i have prepared custom captcha with 5 digit number. When I submit the details, still I getting error page. I have checked various sites in Google but could not find the correct answer.

  1. comments.html - Comment box for comments
  2. captcha.php - Custom captcha with 5 digit code
  3. submit.php - for processing the code
  4. error.html - error page for wrong entry
  5. thank.html - Page on submitting successful

I am unable to sort-out where the mistake is. Kindly help me in this regards. The sources codes of comments.html and submit.php is given below.

=========COMMENTS.HTML==============

<form action="submit.php" method="post"> 
Name: <input type="text" name="name" /> <br>
Email: <input type="text" name="email" /> <br>
Comments:  <textarea  name="coments" />  <br>
Enter Captcha <img src="captcha.php"><input type="text" name="vercode" /> <br>
<input type="submit" name='submit'  onclick="show_confirm()" value="SUBMIT" />
</form>

=============SUBMIT.PHP=================

<?php
session_start(); 
if ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["vercode"]=='') 
       {
    //This page should not be accessed directly. Need to submit the form.
    header('Location: error.html');
    exit;
       }
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

if(empty($name) || empty($email)||empty($comments)) 
    {
    header('Location:error.html');
    exit;
    }
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email]))  
   {
    header('Location:error.html');
    exit;
    }
    $email_from = 'info@xxxxx.com';
$email_subject = "CONTACT FORM";

$email_body="============================
".
"FULL NAME: $name
".
"EMAIL-ID:   $email
".
"COMMENTS:     $comments
".

$to = "info2@xxxxx.com";
$headers = "From: $email_from 
";

mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank.html');
?>

You need

session_start()

at the very top of your submit.php, that starts or resume your session so that you can access $_SESSION

You should check if the form was submitted first, then process the code. Example:

if(isset($_POST['submit'])) { // process stuff }

You didn't show what specific error you get, so I'm just going to link you to this simple PHP-GD captcha that I have used previously in some projects and works like a charm. Is really simple and easy to implement.

Simple PHP-GD captcha image

It looks like it may have something to do with your regex verification always returning false.

You may want to test if the rule you set is correct. Also, I have read on php.net that eregi() is now obsolete in 5.3.0, so maybe use preg_match() with PCRE_CASELESS flag instead ?