how do i add captcha to my email contact form?

Discussion in 'PHP' started by bosanci28, Apr 11, 2012.

  1. #1
    can someone give me a hand with this code,
    i was trying to add captcha code so when people send emails out to enter
    the "captcha code" in the box,i try few options ,but no go!

    also ,after submiting the email how to redirect auto to the index page?

    here is the

    code from contact.php :

    
    <form method="post" action="sendemail.php">
    
    <!-- DO NOT change ANY of the php sections -->
    <?php
    $ipi = getenv("REMOTE_ADDR");
    $httprefi = getenv ("HTTP_REFERER");
    $httpagenti = getenv ("HTTP_USER_AGENT");
    ?>
    
    <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
    <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
    <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
    
    
    Your Name: <br />
    <input type="text" name="visitor" size="35" />
    <br />
    Your Email:<br />
    <input type="text" name="visitormail" size="35" />
    <br /> <br />
    
    
    
    Mail Message:
    <br />
    <textarea name="notes" rows="4" cols="40"></textarea>
    <br />
    <input type="submit" value="Send Mail" />
    <br />
    </form>
    
    PHP:

    and this is from sendemail.php :

    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Sendemail Script</title>
    </head>
    <body>
    
    
    
    <!-- Reminder: Add the link for the 'next page' (at the bottom) --> 
    <!-- Reminder: Change 'YourEmail' to Your real email --> 
    
    <?php
    
    $ip = $_POST['ip']; 
    $httpref = $_POST['httpref']; 
    $httpagent = $_POST['httpagent']; 
    $visitor = $_POST['visitor']; 
    $visitormail = $_POST['visitormail']; 
    $notes = $_POST['notes'];
    $attn = $_POST['attn'];
    
    if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) 
    {
    echo "<h2>Use Back - Enter valid e-mail</h2>\n"; 
    $badinput = "<h2>Feedback was NOT submitted</h2>\n";
    echo $badinput;
    die ("Go back! ! ");
    }
    
    if(empty($visitor) || empty($visitormail) || empty($notes )) {
    echo "<h2>Use Back - fill in all fields</h2>\n";
    die ("Use back! ! "); 
    }
    
    $todayis = date("l, F j, Y, g:i a") ;
    
    $attn = $attn ; 
    $subject = $attn; 
    
    $notes = stripcslashes($notes); 
    
    $message = " $todayis [EST] \n
    Attention: $attn \n
    Message: $notes \n 
    From: $visitor ($visitormail)\n
    Additional Info : IP = $ip \n
    Browser Info: $httpagent \n
    Referral : $httpref \n
    ";
    
    $from = "From: $visitormail\r\n";
    
    
    mail("home@mysite.com", $subject, $message, $from);
    
    ?>
    
    <p align="center">
    Date: <?php echo $todayis ?> 
    <br />
    Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> ) 
    <br />
    
    Attention: <?php echo $attn ?>
    <br /> 
    Message:<br /> 
    <?php $notesout = str_replace("\r", "<br/>", $notes); 
    echo $notesout; ?> 
    <br />
    <?php echo $ip ?> 
    
    <br /><br />
    <a href="index.php">http://www.mysite.com/index.php</a> 
    </p> 
    
    PHP:
    thanks for any help!
     
    bosanci28, Apr 11, 2012 IP
  2. MarPlo

    MarPlo Member

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    48
    #2
    Hi,
    To redirect auto to the index.php after submitting the mail, replace this line in your script:
    mail("home@mysite.com", $subject, $message, $from);
    Code (markup):
    With this code:
    if(mail("home@mysite.com", $subject, $message, $from)) header('Location: /index.php');
    Code (markup):
    About the captcha, it is needed to many changes, adding session, new field in form, cheching the value of session with the value added in form, ...
     
    MarPlo, Apr 12, 2012 IP
  3. bosanci28

    bosanci28 Well-Known Member

    Messages:
    857
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    105
    #3
    hmm,still not redirecting...
    thanks!
     
    bosanci28, Apr 12, 2012 IP
  4. MarPlo

    MarPlo Member

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    48
    #4
    Hi,
    Sorry, the header() function works only if there is not any output to browser (like echo, or html code) before the line where it is added.
    Try with this code:
    if(mail("home@mysite.com", $subject, $message, $from)) echo '<meta http-equiv="refresh" content="0;url=/index.php" />';
    Code (markup):
    which outputs a meta html tag that force refresh to index.php.
     
    MarPlo, Apr 12, 2012 IP
  5. bosanci28

    bosanci28 Well-Known Member

    Messages:
    857
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    105
    #5
    works!,but,

    now,when i receive the email by the test i have done,i know before when i was getting the email in subject it say something ,now it does not say anything,
    is that has anything to do with the code provided?

    also,if possible after submitting some how to say "email sent ,we will reply within 24 hours or less!"
    and then 5 sec later to redirect to main page?!
    can that be added?

    thanks,friend!
     
    bosanci28, Apr 12, 2012 IP
  6. MarPlo

    MarPlo Member

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    48
    #6
    Normally, that code should not affect the email, because it is executed after the email is send.
    To make the redirect after 5 sec., try this:
    if(mail("home@mysite.com", $subject, $message, $from)) echo '<meta http-equiv="refresh" content="5;url=/index.php" />email sent ,we will reply within 24 hours or less!';
    Code (markup):
     
    MarPlo, Apr 12, 2012 IP
  7. bosanci28

    bosanci28 Well-Known Member

    Messages:
    857
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    105
    #7
    works,now,except for the subject line,i guess because the php code is not there in contact.php form.

    thank you.
     
    bosanci28, Apr 12, 2012 IP
  8. raspms

    raspms Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    such nice posting..thanks for sharing..
     
    raspms, Apr 13, 2012 IP