Reputation: 89
I have a contact form at http://www.kaimeramedia.com/derek/Website/contact.php .I checked my PHP code at PHPcodechecker.com and it says there are no syntax errors. I think the beginning code is working in some manor because every time I press submit it makes it to the next validation where I am left with: "Improper email address detected. Please hit your browser back button and try again".
I just want to know if I should be writing the code differently to make sure the email validation goes through and allow the message to be submitted. I'm Not sure if I have something coded backwards. I tried many combinations, but as it stands this is the farthest I have come without a mailform.php error. The code for the mailform.php file is:
<?php
session_start();
$dontsendemail = 0;
$possiblespam = FALSE;
$strlenmessage = "";
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$message = $_REQUEST['message'];
$subject = "Regarding Your Portfolio";$emailaddress = "[email protected]";
// checks if name field is empty
function checkname() {
if (empty($name)) {
die ("You did not enter your name. Please hit your browser back button and try again.");
return 1;
}
}
// checks if the captcha is input correctly
function checkcaptcha() {
if ($_SESSION["pass"] != $_POST["userpass"]) {
die("Sorry, you failed the CAPTCHA. Note that the CAPTCHA is case-sensitive. Please hit your browser back button and try again.");
return 1;
}
}
// checks proper syntax
function checkemail() {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email));
else{
die("Improper email address detected. Please hit your browser back button and try again.");
return 1;
}
}
function spamcheck($field) {
if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) || eregi("\n",$field) || eregi("%0A",$field)){
$possiblespam = TRUE;
}else $possiblespam = FALSE;
if ($possiblespam) {
die("Possible spam attempt detected. If this is not the case, please edit the content of the contact form and try again.");
return 1;
}
}
function strlencheck($field,$minlength,$whichfieldresponse) {
if (strlen($field) < $minlength){
die($whichfieldresponse);
return 1;
}
}
if ($dontsendemail == 0) $dontsendemail = checkcaptcha($email);
if ($dontsendemail == 0) $dontsendemail = checkemail($email);
if ($dontsendemail == 0) $dontsendemail = spamcheck($email);
if ($dontsendemail == 0) $dontsendemail = spamcheck($name);
if ($dontsendemail == 0) $dontsendemail = strlencheck($email,10,"The email address field is too short. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) $dontsendemail = strlencheck($message,10,"The message field is too short. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) $dontsendemail = strlencheck($emailaddress,8,"You have not selected a recipient of your message. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) $dontsendemail = strlencheck($name,3,"The Name field is too short. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) {mail($emailaddress,"Subject: $subject",$message,"From: $name,$email" ); include "thankyou.php";}
?>
I put a dummy email in this code, but the real one is in the mailform.php file If anyone can see where I went wrong it would be of greatly appreciated.
Upvotes: 0
Views: 807
Reputation: 1620
Building on my previous answer, I went all out and cleaned up your script. I clearly have too much time on my hands. Haven't checked if it works. Goodluck!
<?PHP
session_start();
try{
$check = new check();
if(!isset($_REQUEST['Email']))
throw new exception('You did not enter an email address.');
if(!isset($_REQUEST['message']))
throw new exception('You did not enter a message.');
if(!isset($_REQUEST['Name']))
throw new exception('You did not enter a name');
$sender = $_REQUEST['Email'];
$message = $_REQUEST['message'];
$name = $_REQUEST['Name'];
$recipient = '[email protected]';
$subject = 'Regarding Your Portfolio';
if($check->captcha('userpass') == FALSE)
throw new exception('Your captcha is incorrect.');
if($check->spam($sender) == FALSE)
throw new exception('Your email field contains spam.');
if($check->spam($name) == FALSE)
throw new exception('Your name field contains spam.');
if($check->length($sender, 10) == FALSE)
throw new exception('Your email field does not satisfy the minimum character count.');
if($check->length($message, 8) == FALSE)
throw new exception('Your message field does not satisfy the minimum character count.');
if($check->length($name, 3) == FALSE)
throw new exception('Your name field does not satisfy the minimum character count.');
mail($recipient, $subject, $message, "From: $name <$sender>" );
include "thankyou.php";
}catch (Exception $E){
die($E->getMessage());
}
class check{
function captcha($field){
if(isset($_REQUEST[$field])==FALSE){ return false; }
if($_SESSION['pass'] != $_REQUEST[$field]){ return false; }
return true;
}
function email($email){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ return false;}
return true;
}
function spam($field){
if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) || eregi("\n",$field) || eregi("%0A",$field)){ return false; }
return true;
}
function length($field, $min){
if(strlen($field) < $min){ return false; }
return true;
}
}
Upvotes: 2
Reputation: 1620
There are two problems with your script.
Solution: As of PHP5 you can use the following to validate an email or if you want to use the regular expression just add the $email paramater.
function checkemail($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
}else{
die("Improper email address detected. Please hit your browser back button and try again.");
return 1;
}
}
Upvotes: 0