繁体   English   中英

防止垃圾邮件进入我的表单

[英]Prevent spam from my form

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

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

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 } ?>

您可以在表单底部使用reCaptcha,这将阻止这种情况的发生。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM