Jump to content

How to insert Captcha Code


AndreD

Recommended Posts

Ok I am having an issue and can't think of a way to solve it as I only have basic PHP knowledge

 

I am trying to insert code from reCAPTCHA from google into my current form.  The problem is that it requires the method set to verify.php as I already have a form method of contact.php (this is the name of the page for the form)

 

I am pretty lost so if I can get a detailed explanation of how to get this to function properly I would really appreciate it.

 

The code that needs to be inserted in the form is:

<?php require_once('recaptchalib.php');
  $publickey = "your_public_key"; // you got this from the signup page
  echo recaptcha_get_html($publickey); ?>

The code that is supposed to be in verify.php and that I am hoping I can just inject into contact.php is:

 

<?php
  require_once('recaptchalib.php');
  $privatekey = "your_private_key";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
  }
  ?>

 

And finally here is the contact.php page I have:

<?php

// Set email variables
$email_to = 'info@stay-seo.com';
$email_subject = 'Website Inquiry';

// Set required fields
$required_fields = array('fullname','email','comment','phone','address','city' );

// set error messages
$error_messages = array(
    'fullname' => '(X) Enter Your Name',
    'email' => '(X) Enter Your Email',
    'comment' => '(X) Enter a Message',
    'phone' => '(X) Enter Your Phone',
    'address' => '(X) Enter Your St Address',
    'city' => '(X) Enter Your City',
);

// Set form status
$form_complete = FALSE;

// configure validation array
$validation = array();

// check form submittal
if(!empty($_POST)) {
    // Sanitise POST array
    foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
    
    // Loop into required fields and make sure they match our needs
    foreach($required_fields as $field) {        
        // the field has been submitted?
        if(!array_key_exists($field, $_POST)) array_push($validation, $field);
        
        // check there is information in the field?
        if($_POST[$field] == '') array_push($validation, $field);
        
        // validate the email address supplied
        if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
    }
    
    // basic validation result
    if(count($validation) == 0) {
        // Prepare our content string
        $email_content = 'New Website Comment: ' . "\n\n";
        
        // simple email content
        foreach($_POST as $key => $value) {
            if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
        }
        
        // if validation passed ok then send the email
        mail($email_to, $email_subject, $email_content);
        
        // Update form switch
        $form_complete = TRUE;
    }
}

function validate_email_address($email = FALSE) {
    return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}

function remove_email_injection($field = FALSE) {
   return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}



?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Asphalt Laying, Blacktop Construction and Paving, Cement Building and more.</title>
<meta name="description" content="How to Contact City Service Paving for all your, Blacktop Repair, Asphalt Paving, Cement Maintenance needs.">

<link href="CSS/reset.css" rel="stylesheet" type="text/css" />
<link href="CSS/styles.css" rel="stylesheet" type="text/css" />
<link href='http://fonts.googleapis.com/css?family=Montserrat+Alternates:400,700|Alegreya+Sans:400,300italic,300,100italic,100,400italic,500,500italic,700,700italic,800,800italic,900,900italic|Carrois+Gothic+SC' rel='stylesheet' type='text/css'>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-44912802-1', '1800blacktop.pro');
  ga('send', 'pageview');

</script>
</head>

<body>
<img src="images/bg.jpg" id="bg"/>
<div id="wrap">
<header>
<div class="logowrap"><img src="images/logo2.png" class="logo"/></div>
</header>

<nav>
   <ul>
    <li><a href="index.html">HOME</a></li>
    <li><a href="services.html">SERVICES</a></li>
    <li><a href="photos.html">PHOTOS</a></li>
    <li><a href="references.html">REFERENCES</a></li>
    <li><a href="about.php">ABOUT US</a></li>
    <li><a class="current" href="contact.php">FREE QUOTE</a></li>
   </ul>
</nav>

<div id="main">

<h1>REQUEST A QUOTE</h1>
<h3>To contact us for a quote or for more information, please enter your details below.</h3>

<div class="contactlft">
<div id="formWrap">
<div id="form">
<?php if($form_complete === FALSE): ?>

<form action="contact.php" method="post" id="comments_form">



<div id="row">
<div id="input">
<input type="text" id="fullname" class="detail" name="fullname" placeholder="Name" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" /><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>
</div><!-- end .input -->
</div><!-- end .row -->
 <div class="clear"></div>  



<div id="row">
<div id="input">
<input type="text" id="email" class="detail" name="email" placeholder="Email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" /><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>
</div><!-- end .input -->
</div><!-- end .row -->
 <div class="clear"></div>  
 

<div id="row">
<div id="input">
<input type="text" id="phone" class="detail" name="phone" placeholder="Phone Number" value="<?php echo isset($_POST['phone'])? $_POST['phone'] : ''; ?>" /><?php if(in_array('phone', $validation)): ?><span class="error"><?php echo $error_messages['phone']; ?></span><?php endif; ?>
</div><!-- end .input -->
</div><!-- end .row -->
 <div class="clear"></div>  

<div id="row">
<div id="input">
<input type="text" id="address" class="detail" name="address" placeholder="Work St. Address" value="<?php echo isset($_POST['address'])? $_POST['address'] : ''; ?>" /><?php if(in_array('address', $validation)): ?><span class="error"><?php echo $error_messages['address']; ?></span><?php endif; ?>
</div><!-- end .input -->
</div><!-- end .row -->
 <div class="clear"></div>
 
<div id="row">
<div id="input">
<input type="text" id="city" class="detail" name="city" placeholder="Work City" value="<?php echo isset($_POST['address'])? $_POST['city'] : ''; ?>" /><?php if(in_array('city', $validation)): ?><span class="error"><?php echo $error_messages['city']; ?></span><?php endif; ?>
</div><!-- end .input -->
</div><!-- end .row -->
 <div class="clear"></div>
 

<div id="row">
<div id="input">
<textarea id="comment" name="comment" class="mess" placeholder="Type your work description here." scrolling="no">
<?php $blockwords="http://"
?>
<?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?></textarea><?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>
</div><!-- end .input -->
</div><!-- end .row -->
<div class="clear"></div>  

 

<div class="buttonholder">

<input type="submit" id="submit" name="submit" class="submit" value="Send Message" />
</div><!-- end .submit -->

</form>
<?php else: ?>
<p style="font-size:24px; font-family:'Carrois Gothic SC', sans-serif; color:#669; text-shadow: 1px 1px #FFF; margin-top:10px; width:100%; text-align:center; font-weight:bold;">Thank you for your Message!</p>
<script type="text/javascript">
setTimeout('ourRedirect()', 5000)
function ourRedirect(){
    location.href='contact.php'
}
</script>
<?php endif; ?>

</div><!-- end #form -->
</div><!-- end formWrap -->
</div>


<div class="contactrt">

<p>Toll free (In Calif.): 1-800-252-2586<br />
Toll free (Nationwide): 1-800-339-2877<br /><br />
Local Offices/Business Centers:<br /><br />
Los Angeles: 213-232-1030<br />
Mid Cities: 562-696-6288<br />
Anaheim/North Orange County: 714-632-6656<br />
South Orange County: 949-951-3886<br />
San Gabriel Valley: 626-228-0371<br />
San Fernando Valley: 818-217-8078<br />
Riverside/Inland Empire: 951-240-5047<br />
San Diego: 619-272-2348<br />
Sacramento: 916-200-4851<br />
San Jose: 408-462-6487<br />
Oakland: 510-250-0731<br />
Merced/Fresno/Central Valley: 800-339-2877<br />
Fax: 800-878-9952</p>
</div>
<div class="clear"></div>
</div>



<footer>
<p><a href="https://www.facebook.com/pages/City-Service-Paving/247045168732491" target="_blank"><img src="images/facebook.png" class="social hover"/></a>©2014 City Service Paving, Website Design by <a style="text-decoration:none; color:#FF6" href="http://www.monkbuns.com" target="_blank">Monkbuns</a>.<a href="https://twitter.com/CityServicePavi" target="_blank"><img src="images/twitter.png" class="social1 hover"/></a></p>
</footer>
</div>
</body>
</html>

 

again any help would be much appreciated.
 

Edited by AndreD
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.