Code: Select all
<img src="security/securimage_show_reg.php" />Moderator: General Moderators
Code: Select all
<img src="security/securimage_show_reg.php" />Code: Select all
document.getElementById('someimage').src = "somenewimage.jpg";
document.getElementById('somehiddenvar').value = "somenewstring";Code: Select all
<img id="someimage" src="security/securimage_show_reg.php" />yestecktalkcm0391 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" />
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.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";
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.Burrito wrote:that won't satisfy his request to not refresh the page.
Code: Select all
<?php
$res = gnupg_init();
gnupg_addencryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC");
$code = gnupg_encrypt($res, $code);
// Store value to cookie!
?>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.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.
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;
}
?>