php映像中的会话
我正在编写一个表单,该表单在提交之前经过jQuery验证。其中一个字段是验证码 这个想法非常简单,验证码显示为图像,也存储在隐藏字段中。当用户在输入字段中输入验证码时,它应等于隐藏字段的值。这是我的jQuery代码php映像中的会话,php,jquery,hidden-field,Php,Jquery,Hidden Field,我正在编写一个表单,该表单在提交之前经过jQuery验证。其中一个字段是验证码 这个想法非常简单,验证码显示为图像,也存储在隐藏字段中。当用户在输入字段中输入验证码时,它应等于隐藏字段的值。这是我的jQuery代码 <script> $(document).ready(function(){ $("#register").validate({ errorElement: 'div', ignore: 'hidden', rules: { password: {
<script>
$(document).ready(function(){
$("#register").validate({
errorElement: 'div', ignore: 'hidden',
rules: {
password: {
required: true
},
password2: {
required: true, equalTo: "#password"
},
agree: {
required: true
},
captcha: {
required: true, equalTo: "#captcha2"
}
},
messages: {
password2: "Please repeat the same password",
captcha: "The captcha you entered is wrong",
agree: "You have to be at least 16 years old and agree to the terms of service"
}
});
});
</script>
$(文档).ready(函数(){
$(“#寄存器”).validate({
errorElement:'div',忽略:'hidden',
规则:{
密码:{
必填项:true
},
密码2:{
必填项:true,等于:“#密码”
},
同意:{
必填项:true
},
验证码:{
必填项:正确,等号:“#captcha2”
}
},
信息:{
密码2:“请重复相同的密码”,
验证码:“您输入的验证码错误”,
同意:“您必须年满16岁并同意服务条款”
}
});
});
表单的html代码更简单,但我将只显示其中的一部分
<p>
<img src='/includes/captcha.php' />
</p>
<p>
<label for="Captcha">Captcha</label>
<em> *</em><input type="text" name="captcha" id="captcha" size="25"
class="require" minlength="5" />
<input type="hidden" name="captcha2" id="captcha2"
value="<?php echo $_SESSION['captcha'];?>" />
<?php echo $_SESSION['captcha']; ?>
验证码
*
这有点奇怪。当我回显会话['captcha']时,我得到的值与隐藏字段中的值不同。我在回显时得到上一个验证码
假设验证码值是ABC。当我刷新页面时,captcha+隐藏字段变为DEF。但当我回显会话['captcha']时,我仍然会得到ABC
为什么会这样
下面是captcha.php文件的代码
<?php
session_start();
header("Content-type: image/png");
$string = '';
for ($i = 0; $i < 5; $i++) {
// this numbers refer to numbers of the ascii table (lower case)
$string .= chr(rand(97, 122));
}
$_SESSION['captcha'] = $string;
putenv('GDFONTPATH=' . realpath('.'));
$dir = 'musicals';
$image = imagecreatetruecolor(170, 60);
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 129, 180, 79); // green
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir, $_SESSION['captcha']);
imagepng($image);
?>
问题在于您的验证码生成在图像中,并且只有在访问表单后才能访问此图像。您的验证码是在显示表单后生成的,这就是为什么它总是落后一步
除此之外,将验证码解决方案存储在隐藏字段中是个坏主意。个人或机器人只需解析网站的源代码,提取并将其用作验证码解决方案。确保在html代码之前运行captcha.php。它看起来像是captcha.php生成的图像,可能是作为一个img插入到HTML中的。图像是在页面加载(生成HTML)后获得的,因此不会重新生成会话中的验证码
我相信您插入了带有captcha的隐藏元素作为示例。将captcha代码显示为表单中的隐藏元素是一个坏主意。这只是另一个建议,但您不应该将答案存储在页面上,用户可以方便地查看答案。也许给它一个类似会话的ID,它对应于某个地方(例如数据库)以匹配答案。那么您想通过将捕获值写入可读的输入字段来增强“安全性”吗?哇!