title: PHP contact form with Captcha: "failed the anti-spam check"
body: <p>I'm using a Contract form from HTML Contract Form Guide in my website. In testing, I am running my website from my computer using apache and PHP v5.5. Everything seems to work well on my testing server and I can fill out the form, click submit, and land on the "success page". When I upload my site to my hosting service and try to use the contact form, the captcha does not accept my input and returns the message, "failed the anti-spam check". My hosting service is running PHP v5.3. I'm not sure what's changing and why my form no longer works when I upload it. Oh, I am also learning PHP on the fly so I might not fully comprehend what I am being asked so it might take me a little longer to give an accurate reply.</p>
  
  <p><strong>* This issue was solved. I ended up contacting fatcow which is my hosting company. This is their response "I have set the session.save_path in your 'PHP Scripting' and I have increased the memory limit from 32 MB to 128 MB for your PHP scripts." I guess it was a server side issue. I guess I've narrowed it down and when I run to a similar issue I can also assume it is a server side issue. Hope anyone with a similar issue can use this post and decide what to do accordingly.</strong></p>
  
  <p>I ran <code>var_dump()</code> on <code>$_SESSION['FGCF_Captcha_Answer']</code> and <code>$user_answer</code> and both values were equal. This is what I got:</p>
  
  <p>string 'c85757e710d687d24c0b044812d5ee05' (length=32)</p>
  
  <p>string 'c85757e710d687d24c0b044812d5ee05' (length=32)</p>
  
  <p>I am also hosting from a fatcow server and have contacted them to see if they know anything about this issue with reCAPTCHA on their server. Just awaiting their reply. Is it possible that the server I'm running on could experience these things, maybe not a programming question but like I stated above, on my own computer running with apache the form works fine and the only problem I'm having on fatcow is not the entire form, just reCAPTCHA</p>
  
  <p>Here is the captcha code, if anything else is needed please let me know:</p>
  
  <pre><code>&lt;?PHP
      class FGSimpleCaptcha extends FG_CaptchaHandler
      {
  var $error_str;
  var $captcha_varname;
  var $uniquekey;
  
  function FGSimpleCaptcha($captcha_var_name)
  {
      $this-&gt;captcha_varname=$captcha_var_name;
      $this-&gt;uniquekey='KHJhsjsy65HGbsmnd';
  }
  
  /*Add more simple questions here.*/
  function GetSimpleCaptcha()
  {
      $arrQuestions = array(
      "What color is the sky? "=&gt;"blue",
      "What is 1+1=" =&gt; "2",
      "What is the color of grass?"=&gt;"green",
      "Are you a robot? "=&gt;"no",
      "Are you human?"=&gt;"yes");
  
      $question = array_rand($arrQuestions);
      $answer = $arrQuestions[$question];
  
      $_SESSION['FGCF_Captcha_Answer'] = $this-&gt;Md5CaptchaAnswer($answer);
  
      return $question;
  }
  
  function SetFormKey($key)
  {
      $this-&gt;uniquekey = $key;
  }
  function GetKey()
  {
      return $this-&gt;uniquekey;
  }
  function Validate()
  {
      $ret=false;
      if(empty($_POST[$this-&gt;captcha_varname]))
      {
          $this-&gt;error_str = "Please answer the anti-spam question";
          $ret = false;
      }
      else
      {
  
          $scaptcha = trim($_POST[$this-&gt;captcha_varname]);
  
          $scaptcha = strtolower($scaptcha);
  
          $user_answer = $this-&gt;Md5CaptchaAnswer($scaptcha);
  
          if($user_answer != $_SESSION['FGCF_Captcha_Answer'])
          {
              $this-&gt;error_str = "Failed the anti-spam check!";
              $ret = false;
          }
          else
          {
              $ret = true;
          }
      }//else
      return $ret;
  }
  function Md5CaptchaAnswer($answer)
  {
      return md5($this-&gt;GetKey().$answer);
  }
  function GetError()
  {
      return $this-&gt;error_str;
  }
  }
  ?&gt;
  </code></pre>
  
  <p>Here is partial code for the form:</p>
  
  <pre><code>    class FGContactForm
  {
  var $receipients;
  var $errors;
  var $error_message;
  var $name;
  var $email;
  var $message;
  var $from_address;
  var $form_random_key;
  var $conditional_field;
  var $arr_conditional_receipients;
  var $fileupload_fields;
  var $captcha_handler;
  
  var $mailer;
  
  function FGContactForm()
  {
      $this-&gt;receipients = array();
      $this-&gt;errors = array();
      $this-&gt;form_random_key = 'HTgsjhartag';
      $this-&gt;conditional_field='';
      $this-&gt;arr_conditional_receipients=array();
      $this-&gt;fileupload_fields=array();
  
      $this-&gt;mailer = new PHPMailer();
      $this-&gt;mailer-&gt;CharSet = 'utf-8';
  }
  
  function EnableCaptcha($captcha_handler)
  {
      $this-&gt;captcha_handler = $captcha_handler;
      session_start();
  }
  
  function AddRecipient($email,$name="")
  {
      $this-&gt;mailer-&gt;AddAddress($email,$name);
  }
  
  function SetFromAddress($from)
  {
      $this-&gt;from_address = $from;
  }
  function SetFormRandomKey($key)
  {
      $this-&gt;form_random_key = $key;
  }
  function GetSpamTrapInputName()
  {
      return 'sp'.md5('KHGdnbvsgst'.$this-&gt;GetKey());
  }
  function SafeDisplay($value_name)
  {
      if(empty($_POST[$value_name]))
      {
          return'';
      }
      return htmlentities($_POST[$value_name]);
  }
  function GetFormIDInputName()
  {
      $rand = md5('TygshRt'.$this-&gt;GetKey());
  
      $rand = substr($rand,0,20);
      return 'id'.$rand;
  }
  
  
  function GetFormIDInputValue()
  {
      return md5('jhgahTsajhg'.$this-&gt;GetKey());
  }
  
  function SetConditionalField($field)
  {
      $this-&gt;conditional_field = $field;
  }
  function AddConditionalReceipent($value,$email)
  {
      $this-&gt;arr_conditional_receipients[$value] =  $email;
  }
  
  function AddFileUploadField($file_field_name,$accepted_types,$max_size)
  {
  
      $this-&gt;fileupload_fields[] =
          array("name"=&gt;$file_field_name,
          "file_types"=&gt;$accepted_types,
          "maxsize"=&gt;$max_size);
  }
  
  function ProcessForm()
  {
      if(!isset($_POST['submitted']))
      {
         return false;
      }
      if(!$this-&gt;Validate())
      {
          $this-&gt;error_message = implode('&lt;br/&gt;',$this-&gt;errors);
          return false;
      }
      $this-&gt;CollectData();
  
      $ret = $this-&gt;SendFormSubmission();
  
      return $ret;
  }
  
  function RedirectToURL($url)
  {
      header("Location: $url");/* Redirect browser */
  
      /* Make sure that code below does not get executed when we redirect. */
      exit;
  }
  
  function GetErrorMessage()
  {
      return $this-&gt;error_message;
  }
  function GetSelfScript()
  {
      return htmlentities($_SERVER['PHP_SELF']);
  }
  
  function GetName()
  {
      return $this-&gt;name;
  }
  function GetEmail()
  {
      return $this-&gt;email;
  }
  function GetMessage()
  {
      return htmlentities($this-&gt;message,ENT_QUOTES,"UTF-8");
  }
  </code></pre>