douerlin4366 2011-07-25 19:17
浏览 124

CakePHP表单垃圾邮件

I have built a contact form using CakePHP following the tutorial at http://snook.ca/archives/cakephp/contact_form_cakephp

But would like to add a spam protector where the user is presented with a 5 letter character word such as BB42A that is random and the user has to type in before they can submit the form.

I have done some Googling but haven't found anything suitable online.

Any suggestions? Thanks

The one at the bottom of here is quite good: http://mattbrett.com/portfolio/hire/

  • 写回答

4条回答 默认 最新

  • doumen9709 2011-07-25 19:21
    关注

    What you need is called captcha.

    Google search for cakePHP + captcha should come up with some cakePHP plugins. I don't develop in cakePHP, so I can't tell more.

    You can, of course, make your own captcha and then integrate it in your website. To keep it short:

    • Generate a random string;
    • create an image with this string (imagecreate function on php.net);
    • save the string as session variable;
    • compare what user submitted with what's saved in session.

    Code:

    <?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);
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效