开发过程当中,网络安全采取的方法之一,采用验证码功能。一般在注册、登录的程序当中见得比较多。其自己在实现这一功能时,静态页面有一段调用图形验证码的PHP文件,<img src=’imgcode.php’id=’imgcode’/>,验证码是随机生成的,一般将生成的验证码存入到SESSION当中,以便入其它相关验证操作,由于反应到客户端的图形验证码带有一些杂点,显示时难免会有一点视觉上的干拢,看不清完整的验证码,这时得提供一个刷新验证码的功能,以重新生成验证码。考虑不通过刷新整个页面来达到这个效果,采用AJAX重新生成验证码时,发现SESSION值改变了,客户端修改图片的src属性,给它重新赋值却无任何变化,请求响应后,js代码:

var img_obj =  document.getElementById('imgcode');
img_obj.src = 'imgcode.php';
return;

显然显示在客户端上的图形验证码和服务器上的没有得到统一更新。那么在验证操作时,将不可能得到正确地验证。
原因是由于浏览器的缓存问题,如果两次都是同样的值(src=’imgcode.php’),浏览器一般都不会刷新,所以需要给图片连接动一点手脚。将img_obj.src =’imgcode.php’; 改成 

 img_obj.src = 'imgcode.php?timeamp=' + new Date().getTime(); 

加上一个时间值,或是在后面加上一串随机数字,只要保证总是重新刷新即可,再次运行,成功达到局部刷新功能!
其中的AJAX代码,php生成图形验证码的程序代码,网上一搜,有很多,这里不拿出来献丑了。

6 Comments

  1. Fantastic post. I previousally to spend alot of my time water skiing and watching sports. It was probably the best time of my young life and your post kind of reminded me of that time. Thanks

  2. Great information. I previousally to spend alot of my time wakeboarding and being involved in sports. It was quite possible the best time of my life and your info somehow brought back me of that. Thanks

  3. Great article. I previousally to spend alot of my time water skiing and watching games. It was most certainly the most special period of my past and your blog somehow reminded me of that time. Thank You

  4. Lots of specialists state that mortgage loans help a lot of people to live the way they want, just because they are able to feel free to buy necessary things. Furthermore, various banks offer student loan for young and old people.

  5. Omg please, im you’re buddy on here could you email me to chat and give me the courage to maybe tell her because i really wanna talk to you seein as you was in the exact same scenario as i’m, Please i really need some help with things. 🙁

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


*