Last active
December 16, 2015 23:29
-
-
Save JBreit/5513806 to your computer and use it in GitHub Desktop.
A PHP script to validate the contact form on contact.php and either send the user back to the contact page to fix their errors or forgotten data entry or echo a success message that their form was mailed off after they were validated and accepted for not being a spam bot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* reCaptcha spam filter is 3rd party code was provided | |
* by Google and downloaded at http://www.google.com/recaptcha/ | |
* | |
* Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net | |
* AUTHORS: | |
* Mike Crawford | |
* Ben Maurer | |
* | |
*/ | |
require_once('captcha/recaptchalib.php'); | |
$privatekey = "6LclzuASAAAAAGi0i3UlWLoIP0--M_IzucxfL0QT"; | |
$resp = recaptcha_check_answer ($privatekey, | |
$_SERVER["REMOTE_ADDR"], | |
$_POST["recaptcha_challenge_field"], | |
$_POST["recaptcha_response_field"]); | |
/********************************************************************************/ | |
/* | |
* Email validation code written by Jason Breitigan | |
* http://www.innermindco.com | |
* | |
*/ | |
// we already have an empty array $err setup to hold errors | |
$name = (isset($_POST['name'])) ? trim($_POST['name']) : null; | |
$email = (isset($_POST['email'])) ? trim($_POST['email']) : null; | |
$comments = (isset($_POST['comments'])) ? trim($_POST['comments']) : null; | |
// Validate captcha | |
if( ! $resp->is_valid ) $err['captcha'] = 'Captcha Invalid'; | |
// validate user entered a name | |
if( ! $name ) $err['name'] = 'Name Required'; | |
// validate user entered an email address, and that the email address is properly formatted | |
if( ! $email ) $err['email'] = 'Email Required'; | |
elseif ( ! filter_var($email, FILTER_VALIDATE_EMAIL) ) $err['email'] = 'Invalid Email Address Format'; | |
// validate user entered some comments | |
if ( ! $comments ) $err['comments'] = 'Comments Required'; | |
// check if we caught any errors, if we did not catch any errors, we can call the mail function | |
if( ! $err ){ | |
$to = "[email protected]"; | |
$subject = "{$name} contacted you at foobar.com"; | |
$headers = "From: {$email}\r\n" ."X-Mailer: php"; | |
if ( ! @mail($to, $subject, $comments, $headers)){ | |
$err['mail'] = 'An unknown error prevented the message from being sent!'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment