Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    79
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Adding in a PHP Captcha to existing Form

    Hi everyone, I was hoping someone could please shine some light on this problem I'm stuck on. I've found a free PHP Captcha setup at http://www.white-hat-web-design.co.u...hp-captcha.php.

    I already have a php form mailer that sends out the form submission and redirects either to a success page or back to the form page on failure. The first set of code is the existing mailer and the second set of code is the Captcha PHP code. I thought I could just combine the two, but I'm just getting a blank screen after submit.

    Code:
    <?php
    
    // This block converts any HTML code in the text fields from the previous page to standard text and then the returns
    // Do this for each thing they can type into the forum
    
    $textbox = HTMLSpecialChars("$textbox", ENT_QUOTES);
    
    // You would need to put your validation in at this point, check to see if the variables are good and if not, display an error page
    // Javascript on the form would be the easiest way for you to do this without more PHP
    
    // The system is now used to create email content
    
    		
    		$to = "myemail@email.com";
    		$subject = "Website Information Request";
    		$name_field = $_POST['name'];
    		$company_field = $_POST['company'];
    		$city_field = $_POST['city'];
    		$state_field = $_POST['state'];
    		$zip_field = $_POST['zip'];
    		$phone_field = $_POST['phone'];
    		$email_field = $_POST['email'];
    		$comments_field = $_POST['comments'];
    		
    		$body = "From: $name_field \n 
    		Company: $company_field \n 
    		City: $city_field \n 
    		State: $state_field \n 
    		Zip: $zip_field \n 
    		Phone: $phone_field \n 
    		E-Mail: $email_field\n 
    		Message:$comments_field";
    				
    		mail($to, $subject, $body);
    
    		
    if(mail($mail_to, $mail_subject, $mail_body, "From: $email\r\nReply-to: $email"))
    	{ 
    	header("Location: confirmation.shtml"); /* Redirect browser to thank you page */
    	exit;
    	}
    else 	{ 
    	header("Location: contact.shtml"); /* Redirect browser to fail page */
    	exit;
    	}		
    ?>
    Code:
    <?php 
    session_start();
    
    if( isset($_POST['submit'])) {
       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. 
    		echo 'Thank you. Your message said "'.$_POST['message'].'"';
    		unset($_SESSION['security_code']);
       } else {
    		// Insert your code for showing an error message here
    		echo 'Sorry, you have provided an invalid security code';
       }
    } else {
    ?>
    I tried copying/pasting the form code under the //Insert your code for processing the form here... portion of the PHP Captcha code, but thats where I'm getting the blank screen. Any advice is greatly appreciated!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You can combine it, but you're second block is incomplete; it has an open else statement
    PHP Code:
    <?php

    session_start
    ();

    if( isset(
    $_POST['submit']))
    {
       if(isset(
    $_SESSION['security_code']) && $_SESSION['security_code'] == $_POST['security_code']) // This was incorrect, it could have still tossed
        
    {
            
    //echo 'Thank you. Your message said "'.$_POST['message'].'"';
            
    unset($_SESSION['security_code']);

    ########## YOUR MAIL CODE HERE ##########
    // This block converts any HTML code in the text fields from the previous page to standard text and then the returns
    // Do this for each thing they can type into the forum

    $textbox HTMLSpecialChars("$textbox"ENT_QUOTES);

    // You would need to put your validation in at this point, check to see if the variables are good and if not, display an error page
    // Javascript on the form would be the easiest way for you to do this without more PHP

    // The system is now used to create email content

            
            
    $to "myemail@email.com";
            
    $subject "Website Information Request";
            
    $name_field $_POST['name'];
            
    $company_field $_POST['company'];
            
    $city_field $_POST['city'];
            
    $state_field $_POST['state'];
            
    $zip_field $_POST['zip'];
            
    $phone_field $_POST['phone'];
            
    $email_field $_POST['email'];
            
    $comments_field $_POST['comments'];
            
            
    $body "From: $name_field \n 
            Company: $company_field \n 
            City: $city_field \n 
            State: $state_field \n 
            Zip: $zip_field \n 
            Phone: $phone_field \n 
            E-Mail: $email_field\n 
            Message:$comments_field"
    ;
                    
            
    mail($to$subject$body);

            
    if(
    mail($mail_to$mail_subject$mail_body"From: $email\r\nReply-to: $email"))
        { 
        
    header("Location: confirmation.shtml"); /* Redirect browser to thank you page */
        
    exit;
        }
    else     { 
        
    header("Location: contact.shtml"); /* Redirect browser to fail page */
        
    exit;
        }        


    ########## END YOUR MAIL CODE  ##########
        
    }
        else
        {
            
    // Insert your code for showing an error message here
            
    die ('Sorry, you have provided an invalid security code');
       }
    }
    else
    {
        die(
    'You cannot access this page directly!');
    }


    ?>
    Edit:
    I changed the location of the session unset call. Otherwise it will never occur given the location of the header/exit calls.
    Last edited by Fou-Lu; 02-24-2010 at 06:27 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    aejacks8 (02-24-2010)

  • #3
    New Coder
    Join Date
    May 2008
    Posts
    79
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks so much for the reply. I tried using that code and after entering in a form, I get redirected to a page that says "You cannot access this page directly!"

    I see that this is the last "die" on the PHP you added. Does that mean its not even checking the captcha?
    Last edited by aejacks8; 02-24-2010 at 06:48 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, its a problem with the form.
    Post the html for the form please.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    May 2008
    Posts
    79
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks again! HTML below:

    Code:
    <form action="formail2.php" method="post" name="form2" onSubmit="return submitIt(this)"><!--Form Start-->
    	  
    	  <table width="400" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#E9ECFB" class="blueborder">
      <tr>
        <td colspan="2" class="form1" style="padding:10px">Fields marked with a * are required. </td>
      </tr>
      <tr>
        <td width="199" class="form1" align="right">*Name (First &amp; Last):</td>
        <td width="201" class="form1">
        <input type="text" name="name" size="20"></td><!--First Last Name-->
      </tr>
      <tr>
        <td align="right" class="form1">Company:</td>
        <td class="form1">
          <input type="text" name="company" size="20"><!--Company-->
        </td>
      </tr>
      <tr>
        <td align="right" class="form1">City:</td>
        <td class="form1">
          <input type="text" name="city" size="20"><!--City-->
        </td>
      </tr>
      <tr>
        <td align="right" class="form1">
          State:    </td>
        <td class="form1">
          <select name="state"><!--State-->
            <option value="" selected="selected">Choose a state:</option>
            <option value="AL">AL</option>
            <option value="AK">AK</option>
            <option value="AR">AR</option>
            <option value="AZ">AZ</option>
            <option value="CA">CA</option>
            <option value="CO">CO</option>
            <option value="CT">CT</option>
            <option value="DC">DC</option>
            <option value="DE">DE</option>
            <option value="FL">FL</option>
            <option value="GA">GA</option>
            <option value="HI">HI</option>
            <option value="IA">IA</option>
            <option value="ID">ID</option>
            <option value="IL">IL</option>
            <option value="IN">IN</option>
            <option value="KS">KS</option>
            <option value="KY">KY</option>
            <option value="LA">LA</option>
            <option value="MA">MA</option>
            <option value="MD">MD</option>
            <option value="ME">ME</option>
            <option value="MI">MI</option>
            <option value="MN">MN</option>
            <option value="MO">MO</option>
            <option value="MS">MS</option>
            <option value="MT">MT</option>
            <option value="NC">NC</option>
            <option value="ND">ND</option>
            <option value="NE">NE</option>
            <option value="NH">NH</option>
            <option value="NJ">NJ</option>
            <option value="NM">NM</option>
            <option value="NV">NV</option>
            <option value="NY">NY</option>
            <option value="OH">OH</option>
            <option value="OK">OK</option>
            <option value="OR">OR</option>
            <option value="PA">PA</option>
            <option value="RI">RI</option>
            <option value="SC">SC</option>
            <option value="SD">SD</option>
            <option value="TN">TN</option>
            <option value="TX">TX</option>
            <option value="UT">UT</option>
            <option value="VA">VA</option>
            <option value="VT">VT</option>
            <option value="WA">WA</option>
            <option value="WI">WI</option>
            <option value="WV">WV</option>
            <option value="WY">WY</option>
          </select>
        </td>
      </tr>
      <tr>
        <td align="right" class="form1">Zip Code: </td>
        <td class="form1">
          <input type="text" name="zip" size="20"><!--Zip Code-->
        </td>
      </tr>
      <tr>
        <td align="right" class="form1">Phone Number: </td>
        <td class="form1">
          <input type="text" name="phone" size="20"><!--Phone Number-->
        </td>
      </tr>
      <tr>
        <td align="right" class="form1">*Email:</td>
        <td class="form1">
          <input type="text" name="email" size="20"><!--Email Address-->
        </td>
      </tr>
      <tr>
        <td align="right" class="form1">Comments:</td>
        <td class="form1">
          <textarea name="comments" cols="20" rows="3" wrap="VIRTUAL"></textarea><!--Comments Main Textarea-->
        </td>
      </tr>
      
      <tr>
        <td class="form1">&nbsp;</td>
        <td class="form1">&nbsp;</td>
      </tr>  
     
      
      <tr>
      <td align="right" class="form1"><img src="CaptchaSecurityImages.php" /></td>
      <td class="form1">Security Code: 
    	<input id="security_code" name="security_code" type="text" />
      </td>
      </tr>
      
      <tr>
        <td class="form1">&nbsp;</td>
        <td class="form1">&nbsp;</td>
      </tr>
    
      <tr>
        <td class="form1" colspan="2" align="center"><input type="submit" value="Submit" name="B1"> 
        <input type="reset" value="Reset" name="B2"> </td>
      </tr>
      <tr>
        <td class="form1" colspan="2" align="center">&nbsp;</td>
      </tr>
    </table></form>

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Code:
    <input type="submit" value="Submit" name="B1">
    The submit button as been named B1. You'll need to either change the html submit to be 'submit' for its name, or change the PHP code:
    PHP Code:
    if( isset($_POST['submit'])) 
    // would become:
    if (isset($_POST['B1'])) 
    One or the other, I'd rename the submit button name since that makes more sense to me.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    aejacks8 (02-24-2010)

  • #7
    New Coder
    Join Date
    May 2008
    Posts
    79
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Code:
    <input type="submit" value="Submit" name="B1">
    The submit button as been named B1. You'll need to either change the html submit to be 'submit' for its name, or change the PHP code:
    PHP Code:
    if( isset($_POST['submit'])) 
    // would become:
    if (isset($_POST['B1'])) 
    One or the other, I'd rename the submit button name since that makes more sense to me.
    That did it! Thank you so much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •