[英]Reload captcha issues
I am using this code: http://pastebin.com/Q9RXLZEd for my register page.我正在使用此代码: http : //pastebin.com/Q9RXLZEd作为我的注册页面。 I am trying to get the captcha to reload.
我正在尝试重新加载验证码。 The captcha code is a normal making image from PHP file code and the content type is a PNG image.
验证码是从 PHP 文件代码生成的普通图像,内容类型是 PNG 图像。
My current code doesn't have the captcha refresh or anything.我当前的代码没有验证码刷新或任何东西。
Another issue I thought of is, will the code for what the captcha answer is updated?我想到的另一个问题是,验证码答案的代码会更新吗? If it doesn't, then doesn't that mean that the user will always get a captcha failure error if they use the refresh link?
如果没有,那么这是否意味着用户在使用刷新链接时总是会收到验证码失败错误?
Shiv湿婆
Simple.简单的。 Just add a random parameter to the URL of the PHP file generating the captcha (eg
image.php?param=randomvalue
).只需在生成验证码的 PHP 文件的 URL 中添加一个随机参数(例如
image.php?param=randomvalue
)。 That way you can circumvent caching of the captcha image.这样您就可以绕过验证码图像的缓存。
The code to reload the captcha could be something like this:重新加载验证码的代码可能是这样的:
$("#refreshLink").click(function(){
// Create some random string consisting of numbers
var random = (Math.random() + "").substring(2);
// Load a new captcha image by updating the image's src attribute
$("#refresh img").attr("src", "image.php?param=" + random);
});
You'll also have to slightly adjust your HTML code:您还必须稍微调整您的 HTML 代码:
<tr>
<td>Captcha:</td>
<td><div id='refresh'><img src='image.php'></div></td>
<td><a id='refreshLink'>Refresh</a></td>
</tr>
Regarding the second part of your question: As @James already said - if image.php
sets $_SESSION['string']
correctly every time the file is requested, users won't get captcha errors since the variable is only checked when the form is submitted.关于您问题的第二部分:正如@James 已经说过的-如果每次请求文件时
image.php
设置了$_SESSION['string']
,则用户不会收到验证码错误,因为该变量仅在表单时检查被提交。 Session variables can be set regardless of the content type the PHP file actually returns in the end.无论 PHP 文件最终实际返回的内容类型如何,都可以设置会话变量。
Include this within the <head>
tag将其包含在
<head>
标签中
<script type="text/javascript">
$(document).ready(function() {
// refresh captcha
$('img#captcha-refresh').click(function() {
change_captcha();
});
function change_captcha()
{
document.getElementById('captcha').src="captcha.php?rnd=" + Math.random();
}
});
</script>
And this code wherever you want the captcha image and refresh icon (get a suitable refresh image and rename it to refresh.jpg) to appear.在您希望验证码图像和刷新图标(获取合适的刷新图像并将其重命名为 refresh.jpg)的任何位置显示此代码。 Remember this code and the code above (within the
<script>
tag) should be on the same page for the captcha to work.请记住,此代码和上面的代码(在
<script>
标签内)应该在同一页面上,验证码才能工作。
<img src="captcha.php" alt="" id="captcha" />
Type the word:
<input name="user_captcha" type="text" id="captcha-code">
<img src="refresh.jpg" alt="" id="captcha-refresh" />
Then create a file called captcha.php (it generates captcha image in png format, as requested).然后创建一个名为 captcha.php 的文件(它会根据要求生成 png 格式的验证码图像)。
<?php
session_start();
header("Content-type: image/png");
$word_1 = '';
for ($i = 0; $i < 4; $i++)
{
$word_1 .= chr(rand(97, 122));
}
$_SESSION['random_number'] = $word_1;
$dir = "./";
$image = imagecreatetruecolor(165, 50);
$font = "whatever.ttf"; // font style you may give any you have / prefer
$color = imagecolorallocate($image, 0, 0, 0);// color
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image, 0,0, 709, 99, $white);
imagettftext ($image, 22, 0, 45, 30, $color, $dir.$font, $_SESSION['random_number']);
imagepng($image);
?>
Then finally you have to check whether the user input and the text appearing in the captcha match or not by comparing $_POST['user_captcha']
and $_SESSION['random_number']
wherever you are passing the other values of the form.最后,您必须通过比较
$_POST['user_captcha']
和$_SESSION['random_number']
来检查用户输入和出现在验证码中的文本是否匹配,无论您在何处传递表单的其他值。
NB: Do NOT forget to add session_start();注意:不要忘了添加在session_start(); wherever you want the captcha image.
任何你想要验证码图像的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.