Server Side form!

They have: 68 posts

Joined: Dec 2005

I am wanting to create a self-submitting PHP form which validates on the server side both the required fields and also the content of those fields (ie correct email addresses etc). I also want a captcha integrated. Does anyone have a good standard script they have used that I can use? I cant seem to find a good example of the above anywhere!

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

What is "captcha"?

-Greg

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Greg K;218741 wrote: What is "captcha"?

-Greg

http://en.wikipedia.org/wiki/Captcha

Below is a script I use. This file creates a picture, so call it in the HTML like . Then, have a text box where they copy the code. You will compare that to $_SESSION['security_code'].

This script also uses a TrueType font. That is attached.

<?php
    session_start
();
    
   
/*
    * File: CaptchaSecurityImages.php
    * Author: Simon Jarvis
    * Copyright: 2006 Simon Jarvis
    * Date: 03/08/06
    * Updated: 23/11/06
    * Requirements: PHP 4/5 with GD and FreeType libraries
    * Link: <a href="http://www.white-hat-web-design.co.uk/articles/php-captcha.php
" title="http://www.white-hat-web-design.co.uk/articles/php-captcha.php
">http://www.white-hat-web-design.co.uk/articles/php-captcha.php
</a>    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details:
    * <a href="http://www.gnu.org/licenses/gpl.html
" title="http://www.gnu.org/licenses/gpl.html
">http://www.gnu.org/licenses/gpl.html
</a>    *
    */
    
   
class CaptchaSecurityImages {
    
       var
$font = 'monofont.ttf';
    
       function
generateCode($characters) {
         
/* list all possible characters, similar looking characters and vowels have been removed */
         
$possible = '23456789bcdfghjkmnpqrstvwxyz';
         
$code = '';
         
$i = 0;
          while (
$i < $characters) {
            
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
            
$i++;
          }
          return
$code;
       }
    
       function
CaptchaSecurityImages($width='120',$height='40',$characters='6') {
         
$code = $this->generateCode($characters);
         
/* font size will be 75% of the image height */
         
$font_size = $height * 0.75;
         
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
         
/* set the colours */
         
$background_color = imagecolorallocate($image, 255, 255, 255);
         
$text_color = imagecolorallocate($image, 20, 40, 100);
         
$noise_color = imagecolorallocate($image, 100, 120, 180);
         
/* generate random dots in background */
         
for( $i=0; $i<($width*$height)/3; $i++ ) {
            
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
          }
         
/* generate random lines in background */
         
for( $i=0; $i<($width*$height)/150; $i++ ) {
            
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
          }
         
/* create textbox and add text */
         
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
         
$x = ($width - $textbox[4])/2;
         
$y = ($height - $textbox[5])/2;
         
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
         
/* output captcha image to browser */
         
imagejpeg($image);
         
imagedestroy($image);
         
$_SESSION['security_code'] = $code;
       }
    
    }
    
   
$width = isset($_GET['width']) ? $_GET['width'] : '120';
   
$height = isset($_GET['height']) ? $_GET['height'] : '40';
   
$characters = isset($_GET['characters']) ? $_GET['characters'] : '6';
    
   
header('Content-Type: image/jpeg');
   
$captcha = new CaptchaSecurityImages($width,$height,$characters);
    
?>

They have: 68 posts

Joined: Dec 2005

Thanks for that.

I managed to add a captcha to my form which already had javascript and php validation built in. The highlights when an input is in the wrong format by replacing the form with an error message and a javascript back button to return to the form where the values are still entered. However on adding the captcha the values get wiped in all fields when retuning to the form. I managed to work out it is something to do with the session part of the scritp but not sure why its happening. Its not very user friendly if the form gets wiped on making a mistake. Not sure if I need to have the errors pop up underneath the fields or something instead.

How do you integrate the captcha into a server side validated form you use?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I won't be able to tell with your specific case, but here is a quick example on how you can use it.

http://www.pr0gr4mm3r.com/examples/captcha/captcha.php

They have: 10 posts

Joined: Jul 2007

I'm trying this captcha, where do I put this part of the code? In my FormMail.cgi file?
My form is on an HTML page.

<?php

   session_start
();
   if((
$_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
     
// Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
     
unset($_SESSION['security_code']);
   } else {
     
// Insert your code for showing an error message here
  
}
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.