Page 1 of 1
Refresh / Regenerate CAPTCHA
Posted: Fri Jun 02, 2006 2:51 pm
by tecktalkcm0391
I have the captcah code shown by
Code: Select all
<img src="security/securimage_show_reg.php" />
How can I make a link that will not refresh the page, but only regenerate the code.
Posted: Fri Jun 02, 2006 2:55 pm
by Burrito
this sounds like a good call for ajax.
search this forum or read my tutorial on xmlhttp in the tutorials forum.
Posted: Fri Jun 02, 2006 3:01 pm
by feyd
ajax is overkill for this. Have javascript create a new image with a random string added as a query string.
Posted: Fri Jun 02, 2006 3:09 pm
by tecktalkcm0391
can you tell me how? I am not familiar with JS at all
Posted: Fri Jun 02, 2006 3:11 pm
by Burrito
just using js, you'd have to change the image src as well as the captcha code in a hidden form var
ie:
Code: Select all
document.getElementById('someimage').src = "somenewimage.jpg";
document.getElementById('somehiddenvar').value = "somenewstring";
Posted: Fri Jun 02, 2006 4:12 pm
by tecktalkcm0391
one last question do I have to make the image code this for this to work?
Code: Select all
<img id="someimage" src="security/securimage_show_reg.php" />
Posted: Fri Jun 02, 2006 5:11 pm
by PrObLeM
tecktalkcm0391 wrote:one last question do I have to make the image code this for this to work?
Code: Select all
<img id="someimage" src="security/securimage_show_reg.php" />
yes
Posted: Fri Jun 02, 2006 6:23 pm
by bokehman
Burrito wrote:just using js, you'd have to change the image src as well as the captcha code in a hidden form var
ie:
Code: Select all
document.getElementById('someimage').src = "somenewimage.jpg";
document.getElementById('somehiddenvar').value = "somenewstring";
I don't know what you are talking about here! The act of loading a new image should update the captcha code held in the session variable. The captcha string should not be sent to the document in text form at all.
demo here
Posted: Fri Jun 02, 2006 6:50 pm
by Burrito
bokehman wrote:The act of loading a new image should update the captcha code held in the session variable.
then that won't satisfy his request to not refresh the page.
Posted: Fri Jun 02, 2006 7:02 pm
by bokehman
Burrito wrote:that won't satisfy his request to not refresh the page.
Yes it will. Follow my demo link and click "give me an easier image". The act of updating the image also resets the captch string in the session variable. No page load is necessary. Don't forget that the image itself is created by php and so has access to all of PHP's resorces without any other script being accessed.
Posted: Fri Jun 02, 2006 11:24 pm
by tecktalkcm0391
I dunno what you guys are really taking about, but I am storing the data to a cookie in an encypted form using this:
Code: Select all
<?php
$res = gnupg_init();
gnupg_addencryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC");
$code = gnupg_encrypt($res, $code);
// Store value to cookie!
?>
Posted: Sat Jun 03, 2006 4:58 am
by s.dot
Then updating the captcha without a new hit to the server (refresh) won't be possible without an ajax solution. That would require you to update the cookie contents, which can't be done without hitting the server.
Posted: Sat Jun 03, 2006 6:12 am
by bokehman
scottayy wrote:Then updating the captcha without a new hit to the server (refresh) won't be possible without an ajax solution. That would require you to update the cookie contents, which can't be done without hitting the server.
I don't understand what you are talking about (nor why every keeps talking about AJAX). The following is a simple example of captcha that updates the image without refreshing the page. It relies on Javascript but if Javascript is disabled it will still work but forces a page refresh since this is the only way to update the image if javascript is disabled.
Code: Select all
<?php
session_start();
if(isset($_GET['i'])){
// image creation section
captcha_image();
}elseif(isset($_POST['captcha'])){
// validation section
echo(captcha_validate())?'That was correct.<br>':'That was not correct.<br>';
echo '<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Try another?</a>';
exit;
}else{
// form section
echo
'<p><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].
'" onclick="document.getElementById(\'captcha\').src=\'http://'.
$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].
'?i=\' + new Date; return false;">Give me an easier one!</a></p>'."\n".
'<p><img id="captcha" src="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].
'?i='.uniqid().'" alt=""></p>'."\n".
'<form action="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'" method="POST">'."\n".
'<input type="text" name="captcha" ><input type="submit" value="test it"></form>';
}
function captcha_validate()
{
if($_POST['captcha'] == $_SESSION['captcha'])
{
$_SESSION['captcha'] = null;
return true;
}
return false;
}
function captcha_image($length =
{
$fontsize = 5;
$_SESSION['captcha'] = substr(base64_encode(md5(rand())), 0, $length);
$image = imagecreate(($length*imagefontwidth($fontsize))+1, imagefontheight($fontsize)+1);
$background_colour = imagecolorallocate($image, 255,255,255);
$text_shadow = imagecolorallocate($image, 127,127,127);
$text_colour = imagecolorallocate($image, 0,0,0);
imagestring($image, $fontsize, 1, 1, $_SESSION['captcha'], $text_shadow);
imagestring($image, $fontsize, 0, 0, $_SESSION['captcha'], $text_colour);
header ('Content-type: image/png');
imagepng($image);
imagedestroy($image);
exit;
}
?>