Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation image

Status
Not open for further replies.
Mar 11, 2004
127
GB
I've got a guestbook on my site and I'm really fed up of having to amend it hourly because of (ghey) spam bots!

I would like to put on a form validation method. Ideally one like this...
However, it doesn't exactely tell you how to use it.

Can someone offer me tips on this?

Where would the code go, what other files are needed (GD Libary) etc.

Any help is much appreciated.
Ant

My site if anyone wants to check it out is
 
The code isn't going wrong because I don't actually know where to put it.

Does it go in the gbook.htm page? Does it need a file to go in the cgi-bin folder, whats a GD Library?

These are the "instructions" that are with the code...
[instructions]Use this script in your contact form, for you whois query tool or just there where some extra validation is needed. A session will be created inside a dynamic image file (requires GD library). The random value of this image appears inside the generated image. The user has to enter this value into formfield. This value will be checked while processing the form. Without entering this value a form will not be processed.[/instructions]

As you can see it doesn't reference any files, any locations, or anything that is of any use.

Ant

 
have you installed the right pear packages?

if you have then you would include a function like this in the code that generates your form

Code:
session_start(); //at the start of the file

function generateCaptcha() {
 require_once 'Text/CAPTCHA.php';

//nb insert your own path to font here
 $options = array('font_size'=>'20',
                 'font_path'=>'c:/windows/fonts/', 
                 'font_file'=>'ARIAL.TTF');

$c = Text_CAPTCHA::factory('Image');
$retval = $c->init(150, 150, NULL, $options);
if (PEAR::isError($retval)) {
    echo 'Error generating CAPTCHA!';
    exit;
}

// Get CAPTCHA secret passphrase
$_SESSION['phrase'] = $c->getPhrase();

// Get CAPTCHA image (as PNG)
$png = $c->getCAPTCHAAsPNG();
if (PEAR::isError($png)) {
    echo 'Error generating CAPTCHA as png!';
    exit;
}
file_put_contents("c:/windows/temp/".md5(session_id()) . '.png', $png);
return "c:/windows/temp/".md5(session_id()) . '.png';
} 
?>

and this is how you include the object in your form itself
Code:
<form method="post" action ="somepage.php" >
First Name: <input type="text" name="firstname" /> <br/>
Last Name: <input type="text" name="firstname" /> <br/>
Type the word below: <input type="text" name="phrase" /> 
<img src="<?=generateCaptcha()?>"> <br/>
<input type="submit" name="submit" value="submit"/>
</form>

and in somepage.php
Code:
<?
session_start();
if (empty($_SESSION['phrase'])):
  echo "You have arrived here unexpectedly";
  exit;
endif;
if (isset ($_POST['submit'])):
  if ((!empty($_POST['lastname']) && (!empty($_POST['firstname']) && (!empty($_POST['phrase'])):
     if ($_SESSION['phrase'] === trim($_POST['phrase']) ):
       echo "Form is validated";
     else:
       echo "The wrong validation code was typed";
     endif
   else:
    echo "You have not completed the form properly";
   endif;
  endif;
else:
  echo "arrive unexpectedly";
endif;
 
Ok its starting to come together a bit more now.

So I need a seperate php page that will hold the 3rd piece of code from above.

The code that generates the form? The form is just entered as HTML code as far as I'm aware...

Code:
<FORM METHOD="POST" ACTION="/gbook/bnbbook.cgi">
<TABLE BGCOLOR="#000000" CELLPADDING=5 CELLSPACING=1 BORDER=0 WIDTH=500>
<TR>
<TD><span class="content_bold_red">Name:</span></TD>
<TD><INPUT NAME="name" TYPE="TEXT" SIZE=40 MAXLENGTH=100></TD></TR>
<TD><span class="content_bold_red">E-Mail:</span></TD>
<TD><INPUT NAME="signer_email" TYPE=TEXT SIZE=40 MAXLENGTH=100></TD></TR>
<TD><span class="content_bold_red">Location :</span></TD>
<TD><INPUT NAME="location" TYPE="TEXT" SIZE=40 MAXLENGTH=100></TD></TR>
<TD><span class="content_bold_red">Your url:</span></TD>
<TD><INPUT NAME="url" TYPE="TEXT" SIZE=40 MAXLENGTH=100></TD></TR>
<TD><span class="content_bold_red">How you found us:</span></TD>
<TD><INPUT NAME="howfound" TYPE="TEXT" SIZE=40 MAXLENGTH=100></TD></TR>
<TD><span class="content_bold_red">Private:</span></TD>
<TD> <SELECT NAME="private">
     <OPTION VALUE="NO" SELECTED>NO
     <OPTION VALUE="YES">YES
     </SELECT>
	 <span class="content">note: Private messages will not be shown.</span>
</TD></TR>
<TD><span class="content_bold_red">Your message:</span></TD>
<TD><textarea name="message" wrap=virtual rows=6 cols=40></textarea>
<INPUT TYPE="HIDDEN" NAME="required" VALUE="name,message"></TD></TR>

<TD></TD>
<TD>
<INPUT TYPE="image" src="../Top_nav/images/white/white_submit.gif" onmouseover="this.src='../Top_nav/images/blue/blue_submit.gif'" onmouseout="this.src='../Top_nav/images/white/white_submit.gif'" HEIGHT="15" WIDTH="76" BORDER="0" ALT="sign the guest book">

</TD>
</TR>


</TD>

</TR>
</TABLE>
</FORM>

And I take it that the 2nd piece of code, would be added (not all of it) into my forms code above.

So I'm still stuck on:
a) How and were to install a pear package.
b) Where to include the function code.

Thanks for all your help so far.

Ant
 
So I need a seperate php page that will hold the 3rd piece of code from above.

not really. the third piece of code is that which receives the form submission. no reason why this cannot be the same page that the form is on (this is called an auto-processing page) or a different page. this is a design decision completely within your control. from the looks of your form, the third piece of code would currently take the place of "/gbook/bnbbook.cgi" and you will need to write the guest-book update script yourself in php (this is trivial)

the first piece of code goes above your html form and, yes, the relevant part of the second bit of code goes into your form:

Code:
Type the word below: <input type="text" name="phrase" />
<img src="<?=generateCaptcha()?>"> <br/>

so on your questions:
So I'm still stuck on:
a) How and were to install a pear package.
b) Where to include the function code.
answer to (b) is above the html in the form page (and inside <?php ?> braces)
answer to (a) is more complex. the easiest way to do it is to download gopear.php from gopear.org and put in on your webserver. then open the page in your browser and follow the installation instructions.

pear::captcha has some dependencies. you need to install these packages in the following order:

Text_Password
Image_Text
Text_CAPTCHA

the pear graphical front end will handle the installation for you.

if pear is just too much like hassle for you you could try freecap from but beware: there are appear to be a number of users who are having trouble installing the files. On the whole i'd urge you to persevere with pear as once you have got the base pear class installed you will find that pear opens up a vast array of possibilities for rapid application development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top