Forum Moderators: coopster
I appreciate your help.
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.
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);
}
?>
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.
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{
}
?>
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()?>"> <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 ...
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.
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.
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.