Forum Moderators: coopster

Message Too Old, No Replies

In Need Of Captcha Code That Does Not Lose Input

If a user enters wrong code, does not lose form input values

         

oceanwave

1:16 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



Does anyone know of a good captcha script/code for a php form page, that does not lose the information a user inputs if the user enters the wrong captcha characters? I am using one script now on one page, but if the user makes a mistake on the captcha code every item on the form becomes blank again and they must start all over from scratch. My form is kind of long, so this is completely frustrating to any user.

I appreciate your help.

adb64

1:38 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



If the captcha code was correctly entered you know what to do with all the post or get data. Use that same data to pre-fill all the form fields in case the captcha was wrong. On first entry there most likely is no post or get data so the fields fill in blank as desired, the next time they will be filled with the values that were filled-in the first time.

mojomartini

2:16 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



yep... sounds like a case for if/else ...

basically the 'if' would check if the captcha failed. If it did, the script could populate the new form with the values passed by the POST. The 'else' would just take you to the next page to continue the process.

oceanwave

3:32 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



Hello Everyone!

Tried to use <?php echo...?> statements in value fields which kept the values, only to find out the script needs to refresh in order for the captcha box to repopulate. When it refreshes, I lose the values. If I take the refresh statement out of the script, the captcha box is blank with no characters.

d40sithui

4:38 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



How is your current captcha script designed?
When you submit a form, the receiving page should refresh itself in the process.
and what do you mean by"If I take the refresh statement out of the script"?
I made a basic captcha script the other day. Havent really tested it in the real world, but it seems to work ok when i was playing with it.
basically you call it by the <img> tags which in turn have it load a random string, saves in the session, and generate the image with the string. Feel free to modify it or suggest enhancements.

to call it: <img src="captcha.php">


<?
/*
author: v nguyen
date: 11/09/2007
name: captcha.php
desc:
attemps to create a captcha script
*/

/*
starts session
*/
session_start();

/*
if gd library is installed
*/
if(sizeof(gd_info())){

/*
generates random string
*/
$string = sha1(md5(microtime() * mktime()));

/*
shortening string to between 4 and 7 digits
*/
$key = substr($string, 0, rand(4,7) );

/*
assigns to session
*/
$_SESSION['key'] = $key;

/*
makes captcha image
*/
$width = 100;
$height= 30;
$captcha = imagecreatetruecolor($width, $height);

/*
defines random colors
*/
$color0 = imagecolorallocate($captcha, rand(200,255), rand(200,255), rand(200,255));
$color1 = imagecolorallocate($captcha,rand(50,255),rand(50,255),rand(50,255));
$color2 = imagecolorallocate($captcha,rand(50,255),rand(50,255),rand(50,255));
$color3 = imagecolorallocate($captcha, rand(50,255), rand(50,255), rand(50,255));

/*
drawing misc lines
*/
//imageline($captcha,rand(0,50),rand(15,30),rand(80,100),rand(0,30),$color1);
imageline($captcha,rand(50,100),rand(0,30),rand(0,20),15,$color2);
imagearc($captcha, rand(0,70), 15, rand(50,100), rand(50,100), rand(0,75), 360, $color3);

/*
writes key to image
*/
imagestring($captcha, 5, 20, 10, $key, $color0);

/*
output
*/
header("Content-type: image/png");
imagepng($captcha);

}
?>

oceanwave

4:56 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



Hi d40sithui!

Thanks for responding and the code example.

I am using "Boutell's Simple PHP Captcha" script. I now have to add a captcha to form.php that has an action to reg.php. Not exactly sure where the captcha code fits into all of this. I guess the if/else where there is already a page action is where I am at a loss.

d40sithui

6:17 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



i havent used boutell's captcha script. if its anything like the one i showed you, it should be pretty easy to implement. if not will
need to code in accordance to boutell's usage guidelines. anyway this is the basic rundown. you may want to do some additional input check before or after checking the captcha key. good luck!

Example FORM.php


<form action="reg.php" method="POST">

<!-- example input fields -->
First name:<input type="text" name="firstname"><br>
Last Name:<input type="text" name="lastname"><br>

<!-- captcha image -->
<img src="captcha.php">

<!-- input for captcha -->
Enter validation string above:<input type="text" name="key">

<input type="submit" value="submit">
</form>

Example Reg.php


<?

/*
if posted data are set->continue
*/
if(isset($_POST['firstname'] && isset($_POST['lastname']) && isset($_POST['key'])){

/*
if captcha keys match->continue
im using my example captcha script. not sure how boutell stores his/her captcha string.
*/
if($_SESSION['key'] === $_POST['key']){

}

/*
keys do not match-> reload form
*/
else{

}

}

/*
post data not set-> reload form
*/
else{
}

?>

mojomartini

8:05 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



Ok..I tried a simpler way using the same script (Boutells). As a test, I took his Boutells script and inserted a text field in front of everything else (but inside his form tags). In the field I do simple check to see if the contents of that field have a POST value...if it does it means the script obviously was run again or else it would have passed to the new script. If it doesn't, I just echo the contents of the post variable...

So.....Here are the two things I added to test...

<?php
if ($_POST['send']) {
$errors = array();
if ($_POST['captcha']!= $_SESSION['captchacode']) {
$errors[] = "You did not enter the letters shown in the image.";

// I added this line here
$textfield = $_POST['textfield'];?>

then..down below...

<p>
To prove you are a human being, you must enter the lowercase letters shown
below in the field on the right. Thank you for your understanding!
<p>

//I addded these lines from here .....

<input type="text" name="textfield" value="<?php if(isset($_POST['textfield'])){echo $textfield;}else { echo '">';}?>

... to here//

<img style="vertical-align: middle" src="<?php echo captchaImgUrl()?>">&nbsp;&nbsp;<input name="captcha" size="8"/>

----------------------

Basically I just added a text field to simulate a complete form to see if it would work and it seems to. Try it out ...

oceanwave

9:30 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



Thank you d40sithui and mojomartini (good to see you again)!

I think my problem is there is so much code on my reg.php page. Not only does it post variables, it also strips slashes, inserts fields into my database, creates passwords, emails both myself and the user different information, inputs PayPal buttons, etc. Parts of the page are html, and there are many php portions. When I looked at some of the if/else php portions of the captcha script, I didn't know where to feed the code from this very lengthy reg.php page. If it was a simpler page, I probably could follow the examples.

I'm going to try your codes and see if I have better luck. Thanks again.

oceanwave

3:52 am on Nov 24, 2007 (gmt 0)

10+ Year Member



I put this aside for awhile when I couldn't get this to work and came back to it. Unfortunately, I am still having difficulty. I appreciate your suggestions but I am still doing something very wrong. The page keeps submitting to reg.php even when I enter incorrect characters. Spent a good deal of time on Google but the examples all seem to have an action that posts to self (which I was able to do using Boutell's script in another portion of my site).

My problem is that I can't figure out how to stop the first form page from posting to the reg.php page (and stop the reg.php page from continuing the code) when the captcha characters are incorrect.

oceanwave

11:45 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



Just got it to work. Thanks everyone. This was the solution to my problem.

The php captcha codes I found all started working AFTER my form was submitted, since the action to my form pointed to another page. I found a script that added AJAX Javascript to my first php page, so that the form would not submit to the second page UNTIL the captcha code was verified.

Hope this helps someone else.