Problem validating captcha with remote php file

Problem validating captcha with remote php file

Hi,

I have a form that I am using the JQuery Validation plugin to validate incorrect field with a red border.

 I found the following code online to generate a captcha:

<?php
session_start();
$code=rand(1000,9999);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(50, 24);
$bg = imagecolorallocate($im, 68, 147, 183); //background color blue
$fg = imagecolorallocate($im, 255, 255, 255);//text color white
imagefill($im, 0, 0, $bg);
imagestring($im, 5, 5, 5,  $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

I can validate it on the server-side with the following:

//validate captcha is not empty
if(empty($captcha)){
$formok = false;
$errors[] = "Please answer captcha question";
}elseif(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
$formok = true; 
}
else
{
$formok = false;
$errors[] = "Captcha Code Incorrect";
}

But I can't get it to validate on the client-side so that if an incorrect value is enter the red border remains until the correct value is entered.

Several places recommend the demo  http://jquery.bassistance.de/validate/demo/captcha/ but I can't read the content of the remote process.php file from the captcha.js  I don't know enough PHP to write a working process.php.

 I have tried this:

<?php
$request = $_SESSION['code'];
if (isset($_POST['captcha']) && (int)$_POST['captcha'] == $request {
    echo "true";
} else {
    echo "false";
}
?>

and this:

<?php
session_start()
$request = $_SESSION['code'];
if (isset($_POST['captcha']) && (int)$_POST['captcha'] == $request {
    echo "true";
} else {
    echo "false";a
?>

as well as many other attempts but I can't get the right bit of code to get it working.

 Could someone please help me figure this out.

Thanks in advance


    • Topic Participants

    • i.cue