Use AJAX to reload captcha

Perhaps this question has already been asked - but, unfortunately, I could not find satisfactory answers. I will just ask about this particular case and ask the administrators not to delete the question for at least a few days so I can try ...

I have a page. He uses captcha. For instance:

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

This was my first code. This did not work because the browser found it useless to reload the image if the source is the same. My current solution is to pass the get parameter:

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

A javascript variable is var ranNumrandomly generated for each event onclick. It works great, but I don't like this feature if even the incredible case of two numbers is the same twice in a row. Although the random number varies between -50,000 and 50,000 - I still don't like it. And I do not think the method is right. I would like to know a “faster” method using AJAX. I know this is possible. I hope you know how this is possible. ^ In that case, please show me.

Thanks in advance!

By the way, if I conjure cap (t) cha differently, it doesn't matter, the link to the PHP file is correct in my code: I use randomImage.php

EDIT: A random number in JavaScript is only generated so that the image reloads. Captcha.php does not matter for the $ _GET parameter. The string is really random.

EDIT: , , relaod , .

+3
4

, AJAX . javascript "preload" URL. - , . , , .

+5

, ?

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

:

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

PHP.:)

+1
source

You can also get the image from the base64 Ajax request and paste it into the img tag.

Of course, I think this is too much, and the base64 encoded file is about 4/3 of the original size. (A 3 kb image will be about 4kb on base64).

Edit: for img src attribute e.g.

data: images / PNG; base64, base64encodedimage

0
source

Source: https://habr.com/ru/post/1745591/


All Articles