I am working on a comment form for a website and trying to secure against spammers. I have taken the validEmail function from this link.
I am not very experienced at using functions. Is this the proper way to call the function and validate the user’s email address? Any suggestions appreciated. Thank you
$email = $_POST['email'];
if (validEmail($email)) {
$to = 'fsddfsdfa@gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com';
mail($to, $subject, $message, $headers);
print "Form submitted successfully: <br>Your name is <b>".$_POST['cname']."</b> and your email is <b>".$_POST['email']."</b><br>";
} else {
print "There was an error with your form submission.";
}
The function containing
validEmail( $email );should be included into you file:include "whatever.php"(Or, you could copy and paste the function into the same file as your code)
Your
ifstatement should look something like this:In answer to your comment below, you can also write the above like this: