Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
考虑到安全性,在PHP表单验证中应该寻找什么?_Php_Security_Email_Sendmail_Contact Form - Fatal编程技术网

考虑到安全性,在PHP表单验证中应该寻找什么?

考虑到安全性,在PHP表单验证中应该寻找什么?,php,security,email,sendmail,contact-form,Php,Security,Email,Sendmail,Contact Form,我遇到了一个2012年的现成产品。它使用from 2007检查电子邮件字段是否有效(甚至查询DNS服务器以查看域是否有效)。在6岁的时候,问题出现了——它仍然相关吗 一般来说,人们通常应该在mail()脚本中寻找什么才能使其安全,但占用空间最小?Lovell先生的脚本,结合TutWow的PHP插件,是否符合条件 以下是组合的PHP: <?php // Clean up the input values foreach($_POST as $key => $value) { if

我遇到了一个2012年的现成产品。它使用from 2007检查电子邮件字段是否有效(甚至查询DNS服务器以查看域是否有效)。在6岁的时候,问题出现了——它仍然相关吗

一般来说,人们通常应该在
mail()
脚本中寻找什么才能使其安全,但占用空间最小?Lovell先生的脚本,结合TutWow的PHP插件,是否符合条件

以下是组合的PHP:

<?php

// Clean up the input values
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $_POST[$key] = stripslashes($_POST[$key]);

  $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
  if(!$name) {
    $errors[] = "You must enter a name.";
  } else {
    $errors[] = "Name must be at least 2 characters.";
  }
}
if(!$email) {
  $errors[] = "You must enter an email.";
} else if(!validEmail($email)) {
  $errors[] = "You must enter a valid email.";
}
if(strlen($message) < 10) {
  if(!$message) {
    $errors[] = "You must enter a message.";
  } else {
    $errors[] = "Message must be at least 10 characters.";
  }
}

if($errors) {
  // Output errors and die with a failure message
  $errortext = "";
  foreach($errors as $error) {
    $errortext .= "<li>".$error."</li>";
  }
  die("<span class='failure'>The following errors occured:<ul>". $errortext ."</ul></span>");
}

// Send the email
$to = "YOUR_EMAIL";
$subject = "Contact Form: $name";
$message = "$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");

// A function that checks to see if
// an email is valid
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

?>

通过我所有的项目,我发现自己在安全方面是最有帮助的


它有许多部分,包括表单处理、数据库和SQL、会话等等。

也许最安全的方法是向发件人发送电子邮件,要求他们确认他们确实发送了邮件。此外,为了防止您的联系人表单变成垃圾邮件系统,请将每个IP地址可以提交的联系人表单数量限制在合理的范围内,可能每24小时提交一次,前提是回复将通过常规电子邮件完成。

此验证不是我见过的最好的验证。通过快速查看,我注意到:

  • 它使用
    htmlspecialchars()
    而不使用编码参数
  • 它使用
    strip\u tags()
    的原因我看不出来
  • 它使用
    strlen
    而不是
    mb\u strlen
  • 它容易受到标头注入的影响
  • 这不是我见过的最好的
  • 没有CSRF令牌
  • 这不是验证码

根据我的经验,查询dns可能会丢失有效地址。害怕。不会与安全方面对话,但可能会拨打五个正则表达式电话并检查dns???那会很慢的。好主意。是的,通过普通电子邮件回复。你知道有一个现成的脚本具备所有这些功能吗?