简体   繁体   English

使用AJAX重新加载验证码

[英]Use AJAX to reload captcha

This question may have been asked already - but unfortunately, I could not find any satisfactory answers. 这个问题可能已被提出 - 但不幸的是,我找不到任何令人满意的答案。 I will just ask it for my concrete case and ask the admins not to delete the question for at least a few days so I can try it out... 我会问我的具体案例,并要求管理员不要删除这个问题至少几天,所以我可以尝试一下......

I have a page. 我有一个页面。 It uses a captcha. 它使用验证码。 Like so: 像这样:

<?php
session_start(); // the captcha saves the md5 into the session
?>
<img src="captcha.php" onclick="this.src = this.src" />

That was my first code. 这是我的第一个代码。 It did not work, because the browser condsidered it useless to reload an image if the source is the same. 它不起作用,因为如果源是相同的,浏览器认为它无法重新加载图像。 My current solution is to pass a get parameter: 我目前的解决方案是传递一个get参数:

onclick="this.src = 'captcha.php?randomNumber='+ranNum"

The JavaScript variable var ranNum is generated randomly every time the onclick event fires. 每次onclick事件触发时,都会随机生成JavaScript变量var ranNum It works fine, still, I don't like the possibility, if the - though improbable - case of two numbers being the same twice in a row. 它工作正常,但是,我不喜欢这种可能性,如果 - 虽然不太可能 - 两个数字的情况连续两次相同。 Although the random number varies between -50,000 and 50,000 - I still do not like it. 虽然随机数在-50,000到50,000之间变化 - 我仍然不喜欢它。 And I don't think the method is right. 我不认为这种方法是对的。 I would like to know the 'righter' method, by means of AJAX. 我想通过AJAX了解'righter'方法。 I know it's possible. 我知道这是可能的。 I hope you know how it's possible ^^ In that case, please show me. 我希望你知道它是如何可能的^^在这种情况下,请告诉我。

Thanks in advance! 提前致谢!

By the way - if I spell cap(t)cha differently, never mind, the reference to the PHP file is right in my code: I use randomImage.php 顺便说一句 - 如果我拼写cap(t)cha不同,没关系,我的代码中对PHP文件的引用是正确的:我使用randomImage.php

EDIT: The random number in JavaScript is only generated so the image reloads. 编辑:只生成JavaScript中的随机数,以便重新加载图像。 Captcha.php does not care for the $_GET parameter. Captcha.php不关心$ _GET参数。 The string really is random. 字符串确实是随机的。

EDIT: Thus, what I would like to know is how to make the browser relaod the image without passing different get parameters every time the event fires. 编辑:因此,我想知道的是如何使浏览器重新加载图像,而不是每次事件触发时传递不同的get参数。

Unfortunately, AJAX doesn't provide a good way to dynamically load images. 不幸的是,AJAX没有提供动态加载图像的好方法。 Even using the javascript "preload" trick just gets the browser to load each image once per URL. 即使使用javascript“预加载”技巧,浏览器也会每个URL加载一次图像。 The only good way to get the browser to load another image from the same source is to use a different parameter just like you are doing now. 让浏览器从同一个源加载另一个图像的唯一好方法是使用与您现在正在执行的不同的参数。 And, like other answers have stated, timestamp should be sufficient for that. 而且,正如其他答案所述,时间戳应该足够了。

您是否考虑过使用时间戳?

onclick="this.src='captcha.php?ts='+Math.round(new Date().getTime()/1000)"

Just use: 只需使用:

<img src="captcha.php" onclick='this.src = "captcha.php&time=" + new Date().getTime();' />

You can discard the time parameter and generate the random number in PHP. 您可以丢弃time参数并在PHP中生成随机数。 :) :)

You could also get the image from an Ajax request base64 encoded and put it into the img tag too. 您还可以从Ajax请求base64编码中获取图像,并将其放入img标记中。

Of course I think it is overkill and a base64 encoded file is about 4/3 of the original's size. 当然我认为它是矫枉过正的,而base64编码的文件大约是原始大小的4/3。 (A 3 kb image would be about 4kb on base64). (基础64上的3 kb图像约为4kb)。

Edit: to have the img src attribute like 编辑:拥有img src属性

data:image/png;base64,base64encodedimage 数据:图像/ PNG; BASE64,base64encodedimage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM