<?php
session_start();
$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
$str='';
if($_SESSION['errStr'])
{
$str='<div class="error">'.$_SESSION['errStr'].'</div>';
unset($_SESSION['errStr']);
}
$success='';
if($_SESSION['sent'])
{
$success='<h1>Thank you!</h1>';
$css='<style type="text/css">#contact-form{display:none;}</style>';
unset($_SESSION['sent']);
}
?>
<script>
function formValidate()
{
var captcha=document.form.captcha.value;
var num=/^[0-9]+$/;
if(captcha=="")
{
document.getElementById("errCaptcha").innerHTML="Enter Captcha";
}
else if(!captcha.match(num))
{
document.getElementById("errCaptcha").innerHTML="Enter Valid Captcha";
}
else
{
document.getElementById("errCaptcha").innerHTML="";
var ez=1;
}
if(ez==1)
{
return true;
}
else
{
return false;
}
}
</script>
<form id="form3" name="form" action="" onsubmit='return formValidate()' method="post">
<fieldset class="last">
<p>
<label for="captcha"><?php echo $_SESSION['n1']; ?> + <?php echo $_SESSION['n2'];?> =开发者_运维技巧<span class="style1">*</span><span style="color:red" id="errCaptcha"></span></label></td>
<input type="text" name="captcha" id="captcha" />
</fieldset>
<p class="submit"><button type="submit">Send</button></p>
</form>
this is what i am doing, but not able to validate captcha for eg: 2+2=4 but if i write 2+2=10 it does not validate
From what I can tell from your question and code, you seem to be missing at least 1 javascript function which is required to compare the generated CAPTCHA against the one entered by the user. Another function you are missing (unless my JavaScript is that bad) is a function to actually generate the CAPTCHA.
Without these two vital functions the whole script will not work. Finally, is there a particular reason that you are using so many different $_SESSION
variables? Having a bit more formatting in your code would prove to be helpful in the future so that you can read your code better and help us to read it too...
Although it goes against my better judgement this might help you a bit: javascript based captcha's
精彩评论