防止来自我表单的垃圾邮件

时间:2012-03-27 00:38:27

标签: php forms spoofing

我想创建一个发送给朋友表单而不是用于垃圾邮件。

我在网上搜索过,发现这些来源很有用

http://www.nyphp.org/phundamentals/8_Preventing-Email-Header-Injection http://www.nyphp.org/phundamentals/6_Spoofed-Form-Submissions

这段代码是否足够好?

  • 删除所有新行和返回字符,以便添加新消息
  • 它使用会话变量,以便不能欺骗表单


 function nospam($name) 
    {
        return(str_replace(array("\r", "\n", "%OA", "%oa", "%OD", "%od",
        "Content-Type:","BCC:","bcc:", "CC:","cc:"), "", $name));
    }

    //the form posts to itself
if(isset($_POST['submit'])){
    if($_POST['secret'] == $_SESSION['secret']){

        $_POST['email'] = nospam($_POST['email']); 
        $_POST['sendername'] = nospam($_POST['sendername']); 
        $_POST['link'] = nospam($_POST['link']); 
        $_POST['message'] = nospam($_POST['message']); 
        $_POST['senderemail'] = nospam($_POST['senderemail']); 


        $to = $_POST['email'];
        $subject = $_POST['sendername'] . " has sent you this link.";
        $message = "Hi " . $_POST['name'] . ",\n\n";
        $message .= "The following link was sent to you by " .  $_POST['sendername'] . ".\n\n";
        $message .= $_POST['link'] . "\n\n";
        $message .= $_POST['message'] . "\n\n";
        $from = $_POST['senderemail'];
        $headers = "From:" . $from;
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";
    }
    exit;
}else{
    //set the secret variable when the page opens - only email if it exists 
    $secret = md5(uniqid(rand(), true));
    $_SESSION['secret'] = $secret;
?>

<!--html form code here -->

<?php } ?>

1 个答案:

答案 0 :(得分:1)

您可以在表单底部使用reCaptcha来阻止此类事件的发生。

http://vidiame.com/php/how-to-implement-recaptcha-with-your-php-project