CakePHP表单垃圾邮件

时间:2011-07-25 19:17:52

标签: php cakephp

我已根据http://snook.ca/archives/cakephp/contact_form_cakephp

上的教程使用CakePHP构建了一个联系表单

但是想添加一个垃圾邮件保护程序,向用户提供一个5字母的字符单词,如BB42A,这是随机的,用户必须先输入才能提交表单。

我做了一些谷歌搜索但没有在网上找到任何合适的东西。

有什么建议吗?感谢

这里的底部非常好:http://mattbrett.com/portfolio/hire/

4 个答案:

答案 0 :(得分:3)

我建议使用现有的CAPTCHA库或服务而不是自己编辑。没有意义重新发明轮子。

最好的一个是reCAPTCHA。 Here's a good tutorial on implementing reCAPTCHA in Cake.

答案 1 :(得分:1)

你可以使用主动/被动验证码和简单的数学问题,如2 + 3 http://www.dereuromark.de/2010/08/09/how-to-implement-captchas-properly/

您的决定需要多么安全。对于大多数网站而言,这已经足够了。

答案 2 :(得分:1)

实际上 - 我发现击败垃圾箱的最简单方法之一是在每个联系表格中都有隐藏的字段;并且通常垃圾箱会填满它,而人类,因为他们看不到它,将无法。

尝试添加到您的视图中:

//call it something along the lines of 'name' or 'email', and the
//real form field 'x1' or 'x2' etc
$this-Form->input('aformfield', array('class' => 'aformfield');

确保将其隐藏在css中:

.aformfield{display:none;}

在发送电子邮件之前在控制器中,检查隐藏字段是否已填满:

if(!empty($this->data['Model']['aformfield'])){
    $this->Session->setFlash('You shouldn\'t be able to fill out a hidden field');
    $this->redirect($this->referrer());
}

这不是防弹,而且我确信spambots 会绕道而行,但如果你不想做验证码,这是一个很好的起点。

答案 3 :(得分:0)

您需要的是称为验证码。

谷歌搜索cakePHP + captcha应该提供一些cakePHP插件。我没有开发cakePHP,所以我不能说更多。

当然,您可以制作自己的验证码,然后将其整合到您的网站中。 保持简短:

  • 生成随机字符串;
  • 使用此字符串创建图像 (imagecreate function on php.net);
  • 将字符串保存为会话 变量;
  • 比较用户提交的内容与会话中保存的内容。

代码:

<?php
session_start();    

function rand_str($length = 6,
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
    $chars_length = (strlen($chars) - 1);
    $string = $chars{rand(0, $chars_length)};
    // Generate random string
    for ($i = 1; $i < $length; $i = strlen($string))
    {
        // Grab a random character from list
        $r = $chars{rand(0, $chars_length)};
        // Make sure the same two characters don't appear next to each other
        if ($r != $string{$i - 1}) $string .=  $r;
    }
    return $string;
}

header("Content-Type: image/png");
$im = @imagecreate(100, 40) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0); // black
$text_color = imagecolorallocate($im, 255, 255,255); // white

$random_string = rand_str();
$_SESSION['captcha'] = $random_string;

imagestring($im, 5, 5, 5,  $random_string, $text_color);
imagepng($im);
imagedestroy($im);

?>
相关问题