简体   繁体   English

重新加载PHP图像(验证码)

[英]Reload php image (captcha)

This is a captcha image with a link that will reload the picture if the user wants. 这是一个带有验证码的验证码图片,如果用户需要,该链接将重新加载图片。 This code works only in Google Chrome. 此代码仅在Google Chrome中有效。 How do I make it work in other browsers? 如何使其在其他浏览器中起作用?

<img id="captcha" src="captcha.php">

<a id='reload'>Refresh now</a>

$('#reload').click(function(){
    $('#captcha').attr('src','captcha.php')
})

The other browsers probably cache the image. 其他浏览器可能会缓存图像。 Try the following: 请尝试以下操作:

$("#captcha").attr("src", "captcha.php?"+(new Date()).getTime());

Try this using no caching method (BTW a tag need href attribute to be set): 尝试不使用缓存方法(顺便说一句,需要设置标签href属性):

<a id='reload' href='#'>Refresh now</a>




    $('#reload').click(function(){
        var timestamp = new Date().getTime();
        $('#captcha').attr('src','captcha.php?'+timestamp)
    })

It could be browser caching. 可能是浏览器缓存。 In other words the browser sees that it already loaded captcha.php so it does not need to load it again. 换句话说,浏览器看到它已经加载了captcha.php因此不需要再次加载它。

Try appending a query string to the image source that includes the current time. 尝试将查询字符串追加到包含当前时间的图像源。 Since the image source will now be a URL that the browser has not loaded before it will try to reload it. 由于图像源现在将是浏览器尚未加载的URL,因此它将尝试重新加载它。

<img id="captcha" src="captcha.php">

<a id='reload'>Refresh now</a>

$('#reload').click(function(){
    $('#captcha').attr('src','captcha.php?' + (new Date()).getTime());
});

Better yet, set the HTTP headers on captcha.php to ensure the browser will not cache it. 更好的是,在captcha.php上设置HTTP标头,以确保浏览器不会缓存它。

<?php
  // Set headers to NOT cache a page
  header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
  header("Pragma: no-cache"); //HTTP 1.0
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

Source: https://stackoverflow.com/a/4485194/284685 资料来源: https : //stackoverflow.com/a/4485194/284685

Try this: 尝试这个:

DOM DOM

<div id="captcha-container">
    <img src="captcha.php" id="captcha">
</div>

jQuery jQuery的

$('#reload').click(function() {
    $('#captcha').remove();
    $('#captcha-container').html('<img src="captcha.php" id="captcha">');
});

NET Log NET日志

Each time I click Reload, a new request is made. 每次单击“重新加载”,都会发出一个新请求。

GET captcha.php 
200 OK
127.0.0.1:80

GET captcha.php 
200 OK
127.0.0.1:80

GET captcha.php 
200 OK
127.0.0.1:80

Adding in a new img element will cause the browser to reload it. 添加新的img元素将导致浏览器重新加载它。

Since all the answers so far use jQuery , I thought I'd post mine which uses only plain Javascript. 由于到目前为止所有答案都使用jQuery ,所以我想我会发布仅使用纯Javascript的代码。

// Helper function to attach event listener functions to HTML elements.
function attach(el, event, fun) {
  if (el.addEventListener) {
    el.addEventListener(event, fun);
  }
  else {
    el.attachEvent("on"+event, fun);
  }
}

// Find the <a id='reload'> element.
var id_reload = document.getElementById("reload");
if (null !== id_reload) {
  // Find the <img id='captcha'> element. We assume this works, so no check against null.
  var id_captcha = document.getElementById("captcha");
  attach(id_reload, "click", function() {
    id_captcha.src = "captcha.php?" + (new Date()).getTime();
  });
}

If the src of the image keep the same, the browser won't send a new HTTP request to the server. 如果图像的src保持不变,则浏览器将不会向服务器发送新的HTTP请求。 So change the src by adding a useless timestamp. 因此,通过添加无用的时间戳来更改src。

In browser javascript: 在浏览器javascript中:

var capImage = document.getElementById("captcha-image");  
capImage.addEventListener("click", function () {  
    capImage.src = "/captcha?timestamp=" + (new Date()).getTime();  
}); 

This is a demo for refresh captcha image. 这是刷新验证码图像的演示。

You also need to remove the timestap because it will always concatenate with the previous source url. 您还需要删除时间戳记,因为它将始终与先前的源URL连接在一起。 you might run into problems when a user clicks many times on the reload button. 当用户多次单击重新加载按钮时,您可能会遇到问题。 So it is better to remove the previous timestamp from the url before appending a new one. 因此,最好在附加新时间戳之前从URL中删除先前的时间戳。

$('#reload').on('click', function(){
    var img=$('#captchaimage');
    var src=img.attr('src');
    var i=src.indexOf('?dummy=');
    src=i!=-1?src.substring(0,i):src;
    d = new Date();
    img.attr('src',src+'?dummy='+d.getTime());
});

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

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