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.
* 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.
I ran var_dump()
on $_SESSION['FGCF_Captcha_Answer']
and $user_answer
and both values were equal. This is what I got:
string 'c85757e710d687d24c0b044812d5ee05' (length=32)
string 'c85757e710d687d24c0b044812d5ee05' (length=32)
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
Here is the captcha code, if anything else is needed please let me know:
<?PHP
class FGSimpleCaptcha extends FG_CaptchaHandler
{
var $error_str;
var $captcha_varname;
var $uniquekey;
function FGSimpleCaptcha($captcha_var_name)
{
$this->captcha_varname=$captcha_var_name;
$this->uniquekey='KHJhsjsy65HGbsmnd';
}
/*Add more simple questions here.*/
function GetSimpleCaptcha()
{
$arrQuestions = array(
"What color is the sky? "=>"blue",
"What is 1+1=" => "2",
"What is the color of grass?"=>"green",
"Are you a robot? "=>"no",
"Are you human?"=>"yes");
$question = array_rand($arrQuestions);
$answer = $arrQuestions[$question];
$_SESSION['FGCF_Captcha_Answer'] = $this->Md5CaptchaAnswer($answer);
return $question;
}
function SetFormKey($key)
{
$this->uniquekey = $key;
}
function GetKey()
{
return $this->uniquekey;
}
function Validate()
{
$ret=false;
if(empty($_POST[$this->captcha_varname]))
{
$this->error_str = "Please answer the anti-spam question";
$ret = false;
}
else
{
$scaptcha = trim($_POST[$this->captcha_varname]);
$scaptcha = strtolower($scaptcha);
$user_answer = $this->Md5CaptchaAnswer($scaptcha);
if($user_answer != $_SESSION['FGCF_Captcha_Answer'])
{
$this->error_str = "Failed the anti-spam check!";
$ret = false;
}
else
{
$ret = true;
}
}//else
return $ret;
}
function Md5CaptchaAnswer($answer)
{
return md5($this->GetKey().$answer);
}
function GetError()
{
return $this->error_str;
}
}
?>
Here is partial code for the form:
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->receipients = array();
$this->errors = array();
$this->form_random_key = 'HTgsjhartag';
$this->conditional_field='';
$this->arr_conditional_receipients=array();
$this->fileupload_fields=array();
$this->mailer = new PHPMailer();
$this->mailer->CharSet = 'utf-8';
}
function EnableCaptcha($captcha_handler)
{
$this->captcha_handler = $captcha_handler;
session_start();
}
function AddRecipient($email,$name="")
{
$this->mailer->AddAddress($email,$name);
}
function SetFromAddress($from)
{
$this->from_address = $from;
}
function SetFormRandomKey($key)
{
$this->form_random_key = $key;
}
function GetSpamTrapInputName()
{
return 'sp'.md5('KHGdnbvsgst'.$this->GetKey());
}
function SafeDisplay($value_name)
{
if(empty($_POST[$value_name]))
{
return'';
}
return htmlentities($_POST[$value_name]);
}
function GetFormIDInputName()
{
$rand = md5('TygshRt'.$this->GetKey());
$rand = substr($rand,0,20);
return 'id'.$rand;
}
function GetFormIDInputValue()
{
return md5('jhgahTsajhg'.$this->GetKey());
}
function SetConditionalField($field)
{
$this->conditional_field = $field;
}
function AddConditionalReceipent($value,$email)
{
$this->arr_conditional_receipients[$value] = $email;
}
function AddFileUploadField($file_field_name,$accepted_types,$max_size)
{
$this->fileupload_fields[] =
array("name"=>$file_field_name,
"file_types"=>$accepted_types,
"maxsize"=>$max_size);
}
function ProcessForm()
{
if(!isset($_POST['submitted']))
{
return false;
}
if(!$this->Validate())
{
$this->error_message = implode('<br/>',$this->errors);
return false;
}
$this->CollectData();
$ret = $this->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->error_message;
}
function GetSelfScript()
{
return htmlentities($_SERVER['PHP_SELF']);
}
function GetName()
{
return $this->name;
}
function GetEmail()
{
return $this->email;
}
function GetMessage()
{
return htmlentities($this->message,ENT_QUOTES,"UTF-8");
}
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. With some help I was able to narrow 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.