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

更多相关文章

  1. 有没有办法从指定的网站找到某种颜色的图像?
  2. 从浏览器中删除发送到服务器的标题。
  3. 为什么cron工作不是从浏览器工作,但从终端工作完美
  4. 如何递归将特色图像应用于Wordpress中的子页面?
  5. 如何在没有任何扩展名的php中保存图像
  6. php调整图像大小
  7. php exec文件从终端运行,而不是从浏览器运行
  8. 为什么file_put_contents在从浏览器运行时有权限问题?
  9. 从JSON字符串/数组中提取第一个图像

随机推荐

  1. 高焕堂《android从程序员到架构师之路》
  2. Android底層系統服務開發:以MediaRecorder
  3. Android***测试学习手册(一)Android 安全入
  4. android JNIEnv实现
  5. Android(安卓)xml文件的颜色配置
  6. Android后台杀死系列之三:LowMemoryKiller
  7. Android(安卓)opencv库使用遇到的坑
  8. Android(安卓)常用RGB值及名称
  9. Android 进程间通信之LocalSocket
  10. 无法安装android sdk