[英]PHP Contact Form validation isn't working
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. 我在http://www.kaimeramedia.com/derek/Website/contact.php上有联系表。我在PHPcodechecker.com上检查了我的PHP代码,并说没有语法错误。 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.
我尝试了许多组合,但就目前而言,这是我所遇到的最遥远的尝试,并且没有mailform.php错误。 The code for the mailform.php file is:
mailform.php文件的代码为:
<?php
session_start();
$dontsendemail = 0;
$possiblespam = FALSE;
$strlenmessage = "";
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$message = $_REQUEST['message'];
$subject = "Regarding Your Portfolio";$emailaddress = "me@website.com";
// 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. 我在这段代码中放入了一封虚拟电子邮件,但真正的电子邮件在mailform.php文件中。如果有人可以看到我出了错,那将非常感谢。
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 = 'test@test.com';
$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;
}
}
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. 解决方案:从PHP5开始,您可以使用以下代码来验证电子邮件,或者如果您想使用正则表达式,只需添加$ email参数。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.