Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    Hi,

    I really need to add a captcha to this PHP process form?
    How do I go about doing this?

    Code:
    <?php
    
    // Define some constants
    define( "RECIPIENT_NAME", "John Smith" );
    define( "RECIPIENT_EMAIL", "john@example.com" );
    define( "EMAIL_SUBJECT", "Visitor Message" );
    
    // Read the form values
    $success = false;
    $senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";
    $senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
    $message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
    
    // If all values exist, send the email
    if ( $senderName && $senderEmail && $message ) {
      $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
      $headers = "From: " . $senderName . " <" . $senderEmail . ">";
      $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
    }
    
    // Return an appropriate response to the browser
    if ( isset($_GET["ajax"]) ) {
      echo $success ? "success" : "error";
    } else {
    ?>
    <html>
      <head>
        <title>Thanks!</title>
      </head>
      <body>
      <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
      <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
      <p>Click your browser's Back button to return to the page.</p>
      </body>
    </html>
    <?php
    }
    ?>
    ??

    Thanks in advance!

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,800
    Member #
    5580
    Liked
    726 times
    It looks like you're using AJAX?
    That changes things a little bit.

    Look here first:
    http://www.google.com/#sclient=psy-a...w=1440&bih=678

    Specifically, you may want to use this script:
    http://www.php-developer.org/ajaxrecaptcha/

    Not that there's anything wrong with yours, it's just that it might be easier to start with
    an existing script than to attempt to add captcha to the one you have.


  4. #3
    Junior Member Amal Francis's Avatar
    Join Date
    Oct 2011
    Location
    Thodupuzha, India
    Posts
    9
    Member #
    29779

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Member #
    27888
    You can use the script

    <html>
    <head>
    <title>Sample Email Form</title>
    </head>
    <body>

    <script>
    function checkForm() {
    if (document.forms.myphpform.elements['yname'].value.length == 0) {
    alert('Please enter a value for the "Name" field');
    return false;
    }
    if (document.forms.myphpform.elements['email'].value.length == 0) {
    alert('Please enter a value for the "Email" field');
    return false;
    }
    if (document.forms.myphpform.elements['message'].value.length == 0) {
    alert('Please enter a value for the "Message" field');
    return false;
    }

    return true;
    }
    </script>
    <form action="?done=1" method="post" name="myphpform" onSubmit="return checkForm()" >
    <table border=0>
    <tr>
    <td>Your Name:</td>
    <td>
    <input type="text" name="yname" size="50" maxlength="50" value="" /></td>
    </tr>
    <tr>
    <td>Your Email:</td>
    <td>
    <input type="text" name="email" size="50" maxlength="50" value="" /></td>
    </tr>
    <tr>
    <td>Message:</td>
    <td>
    <input type="text" name="message" size="50" maxlength="50" value="" /></td>
    </tr>
    <tr>
    <td>Are you a human being?</td>
    <td>
    <?php

    @require_once('recaptchalib.php');
    $publickey = "YOUR-PUBLIC-KEY";
    $privatekey = "YOUR-PRIVATE-KEY";

    $resp = null;
    $error = null;

    # are we submitting the page?
    if ($_POST["submit"]) {
    $resp = recaptcha_check_answer ($privatekey,
    $_SERVER["REMOTE_ADDR"],
    $_POST["recaptcha_challenge_field"],
    $_POST["recaptcha_response_field"]);

    if ($resp->is_valid) {
    $to="you@example.com";
    $subject="Feedback from example.com";
    $body=" Message via webform:

    Name: " .$_POST["yname"] . "\n

    Email: " .$_POST["email"] . "\n

    Message: " .$_POST["message"] . "\n";
    /* send email */
    mail($to,$subject,$body);
    echo "

    Email sent!

    ";
    exit(1);

    } else {
    echo "Sorry cannot send email as you've failed to provide correct captcha! Try again...";
    }
    }
    echo recaptcha_get_html($publickey, $error);
    ?>
    <td/>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>
    <input type="submit" name="submit" value="submit" /></td>
    </tr>
    </table>
    </form>

    </body>
    </html>

  6. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Member #
    34880
    TO Create a simple Captcha form, then here is a small guide:

    ===== 1 STEP ======
    in your FTP folder (where you need), put a font file (for example, this: http://www.webpagepublicity.com/free...Anklepants.ttf )
    then create a file (called captcha.php) and paste the below code inside it (then put that captcha.php in the same ftp folder):

    Code:
    <?php
    session_start();
    
    // generate random number and store in session
    $randomnr = rand(1000, 9999);
    $_SESSION['randomnr2'] = md5($randomnr);
    //generate image
    $im = imagecreatetruecolor(100, 38);
    //colors:
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 200, 35, $black);
    
    // -------------      your fontname    -------------
    $font = 'Anklepants.ttf';
    
    //draw text:
    imagettftext($im, 35, 0, 22, 24, $grey, $font, $randomnr);
    
    imagettftext($im, 35, 0, 15, 26, $white, $font, $randomnr);
    
    // prevent client side  caching
    header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-rev?lidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    
    //send image to browser
    header ("Content-type: image/gif");
    imagegif($im);
    imagedestroy($im);
    ?>
    ===== 2 STEP ======

    then in any page (where you want to implement the captcha) put this code somewhere inside that page (but of course, in the last part of this code, there is a sample php fucntion to make an sample action if code is entered correctly. So you should know a more php programming to execute your desired fucntions when Captcha is correct):
    Code:
    <form method="post" action=""> <img src="captcha.php" />
     <input class="input" type="text" name="codee" />
     <input type="submit" value="Submit" />
    </form>
    
    <?php
    session_start();
    if (md5($_POST['codee']) == $_SESSION['randomnr2']) {
     // here you  place code to be executed if the captcha test passes
      echo "YES. Do Something function1";
    }
    
    else {
     // here you  place code to be executed if the captcha test fails
      echo "No.  Do Something function2";
    }
    ?>


Remove Ads

Posting Permissions

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

Search tags for this page

how to add captcha code in php form

,
how to add captcha to a php form
,
how to add captcha to my working php web form
,
how to add captcha to pre existing php form
,

insert captcha in php form

Click on a term to search for related topics.
All times are GMT -6. The time now is 09:57 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com