I read one of SitePoint’s articles regarding anti-spam and user authentication. I have made a custom e-mail contact form using the “honeypot” method of hiding a field with CSS that only a spam bot should be able to see (and subsequently try to fill). This is for a client whose site will be low traffic and only PHP is being utilized on the site (no SQL of any kind).
I’m relatively new to PHP so I would appreciate it if someone could take some time to give me some feedback.
The contact form: http://nupper.heliohost.org/version3/contact.php
Here’s the PHP:
//Initialize $errors array and $vars
$fn = $ln = $e = $e_raw = $m = NULL;
$errors = array();
//http://www.regular-expressions.info/email.html
$regex_email = '!^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$!i';
if(isset($_POST['submitted']) AND empty($_POST['e_tob'])) { //Begin SUBMITTED 'if'
//Validate first name
if(get_magic_quotes_gpc()) {
if(!empty($_POST['fn'])) {
$fn = strip_tags(trim(stripslashes($_POST['fn'])));
} else {
$errors['fn'] = ' You forgot to enter your first name.';
}
} else {
if(!empty($_POST['fn'])) {
$fn = strip_tags(trim($_POST['fn']));
} else {
$errors['fn'] = ' You forgot to enter your first name.';
}
}
//Validate last name
if(get_magic_quotes_gpc()) {
if(!empty($_POST['ln'])) {
$ln = strip_tags(trim(stripslashes($_POST['ln'])));
} else {
$errors['ln'] = ' You forgot to enter your last name.';
}
} else {
if(!empty($_POST['ln'])) {
$ln = strip_tags(trim($_POST['ln']));
} else {
$errors['ln'] = ' You forgot to enter your last name.';
}
}
//Validate e-mail
if(get_magic_quotes_gpc()) {
if(!empty($_POST['e'])) {
$e_raw = strip_tags(trim(stripslashes($_POST['e'])));
if(preg_match($regex_email, $e_raw)) {
$e = $e_raw;
$e_raw = NULL;
} else {
$errors['e_regex'] = ' Please enter a valid e-mail address.';
}
} else {
$errors['e'] = ' You forgot to enter your e-mail address.';
}
} else {
if(!empty($_POST['e'])) {
$e_raw = strip_tags(trim($_POST['e']));
if(preg_match($regex_email, $e_raw)) {
$e = $e_raw;
$e_raw = NULL;
} else {
$errors['e_regex'] = ' Please enter a valid e-mail address.';
}
} else {
$errors['e'] = ' You forgot to enter your e-mail address.';
}
}
//Validate message
if(get_magic_quotes_gpc()) {
if(!empty($_POST['m'])) {
$m = strip_tags(trim(stripslashes($_POST['m'])));
} else {
$errors['m'] = ' You forgot to enter a message.';
}
} else {
if(!empty($_POST['m'])) {
$m = strip_tags(trim($_POST['m']));
} else {
$errors['m'] = ' You forgot to enter a message.';
}
}
} elseif(isset($_POST['submitted']) AND !empty($_POST['e_tob'])) { //!trela toB
header('Location: http://www.youtube.com/watch?v=e1dvSlvZLG8');
ob_end_clean();
die();
} //End SUBMITTED 'if'
if(isset($_POST['submitted']) && !$errors) {
//Set timezone for Edmonton
date_default_timezone_set('America/Edmonton');
//Prepare the e-mail
$time = date('g:i a (T)');
$date = date('l F j, Y');
$fullname = $fn . ' ' . $ln;
$body = "blah blah blah";
$body = wordwrap($body, 70);
//Send the e-mail
mail('me@email.com', 'Contact Form Submission', $body, "From:no-reply@email.com");
echo "\ \ \ \ \ ";
echo '<p style="margin:15px 0;">You have successfully sent your message!</p>';
//Reset $vars & $_POST array to reset the form data
$fn = $ln = $e = $e_raw = $m = NULL;
$_POST = array();
} elseif(isset($_POST['submitted']) && $errors) {
echo "\ \ \ \ \ ";
echo '<p style="margin:15px 0; color:#FFFF00;">Oops! You made a small mistake in the form!</p>';
} else {
echo "\ \ \ \ \ ";
echo '<p style="margin:15px 0;">Please fill out every field in the form below.</p>';
}
The HTML form:
<form action="" method="post">
<p><input type="text" name="fn" value="<?php if_isset($fn); ?>" size="20" maxlenght="40" /><span style="color:#FFFF00; font-weight:900;"><?php if_isset('fn', $errors, 'TRUE'); ?></span></p>
<p><input type="text" name="ln" value="<?php if_isset($ln); ?>" size="40" maxlength="40" /><span style="color:#FFFF00; font-weight:900;"><?php if_isset('ln', $errors, 'TRUE'); ?></span></p>
<p><input type="text" name="e" value="<?php if_isset($e); ?>" size="40" maxlength="80" /><span style="color:#FFFF00; font-weight:900;"><?php if_isset('e', $errors, 'TRUE'); if_isset('e_regex', $errors, 'TRUE'); ?></span></p>
<p class="retlif_tob">Confirm E-mail: <input type="text" name="e_tob" size="40" maxlength="80" /></p>
<br />
<p><textarea name="m" rows="10" cols="60"><?php if_isset($m); ?></textarea><span style="color:#FFFF00; font-weight:900; vertical-align:top;"><?php if_isset('m', $errors, 'TRUE'); ?></span></p>
<br />
<input type="hidden" name="submitted" value="TRUE" />
<p><input type="submit" name="submit" value="Send E-mail" id="submit" alt="Send E-mail to the Clay Guys" title="Send E-mail to the Clay Guys" /></p>
</form>
The custom function used in the HTML form:
// if_isset() -- Custom if(isset()) function
function if_isset($var, $array = NULL, $true='FALSE') {
if($true == 'FALSE'){
if(isset($var)) echo $var;
} else {
if(isset($array[$var])) echo $array[$var];
}
}
Thank you