Refresh / Regenerate CAPTCHA

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Refresh / Regenerate CAPTCHA

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

this sounds like a good call for ajax.

search this forum or read my tutorial on xmlhttp in the tutorials forum.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ajax is overkill for this. Have javascript create a new image with a random string added as a query string.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

can you tell me how? I am not familiar with JS at all
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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";
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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" />
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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!
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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;
}

?>
Post Reply