使用php和jquery进行验证码动态更新
Would appreciate any piece of advice on this. Need to dynamically renew the captcha image generated by php-script, when the "captcha_refresh" is clicked. Below is the way I tried to achieve it. Unfortunately this approach works fine only with Google Chrome, and other browsers seem not to understand this.
非常感谢任何关于此的建议。单击“captcha_refresh”时,需要动态更新php脚本生成的验证码图像。以下是我尝试实现它的方式。不幸的是,这种方法仅适用于谷歌浏览器,其他浏览器似乎无法理解这一点。
HTML
<img class="captcha_image" src="http://localhost/shop/create_image.php"/>
<a class="captcha_refresh" href="http://localhost/shop/create_image.php">
<img src="http://localhost/shop/images/captcha_refresh.png"/>
</a>
JQuery
$('a.captcha_refresh').live('click', function (event) {
event.preventDefault();
$('img.captcha_image').attr('src', $(this).attr('href'));
});
The PHP prints the image:
PHP打印图像:
header("Content-Type: image/jpeg");
ImageJpeg($captchaImage);
The approach like: $('#someButton').click(function() { $('#someDiv').load('captcha.php'); });
suggested at PHP: Reloading the captcha image from javascript is not working. Thanks for attention.
方法如:$('#someButton')。click(function(){$('#someDiv')。load('captcha.php');});在PHP上建议:从javascript重新加载验证码图片是行不通的。谢谢你的关注。
2 个解决方案
#1
1
Add a random query string to the image source:
将随机查询字符串添加到图像源:
$('img.captcha_image').attr('src', $(this).attr('href') + '?rand=' + Math.random());
You should also send Cache-Control
and Expires
headers with the image and instruct the browser that it should not cache the image:
您还应该向图像发送Cache-Control和Expires标头,并指示浏览器不应该缓存图像:
<?php
header("Content-Type: image/jpeg");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// rest of your image generation code
更多相关文章
- 有没有办法从指定的网站找到某种颜色的图像?
- 从浏览器中删除发送到服务器的标题。
- 为什么cron工作不是从浏览器工作,但从终端工作完美
- 如何递归将特色图像应用于Wordpress中的子页面?
- 如何在没有任何扩展名的php中保存图像
- php调整图像大小
- php exec文件从终端运行,而不是从浏览器运行
- 为什么file_put_contents在从浏览器运行时有权限问题?
- 从JSON字符串/数组中提取第一个图像