Php图像验证码生成器

时间:2014-06-18 12:11:47

标签: php captcha spam

我的网站脚本用于生成验证码代码以阻止垃圾邮件机器人,但它似乎被绕过了。该网站采用VLd个人制作,其密码非常弱。它很容易通过机器人传递,以便在一天内获得3K注册。

我已将原始代码修改为下面的代码,但它仍然被绕过,尽管它们大大减少了垃圾帐户的数量

<?php


session_start();


//------------------------------------------
// Generate captcha
//------------------------------------------
captcha($_SESSION['captcha']);


//------------------------------------------
// Generate captcha
//------------------------------------------
function captcha($magic_number)
{
    $height = 55;
    $width = 280;

    if (extension_loaded('gd'))
    {
        // create the image
        $image = imagecreatetruecolor($width, $height);

        // background
        $color1 = imagecolorallocate($image, mt_rand(255, 255), mt_rand(255, 255), mt_rand(255, 255));
        $color2 = imagecolorallocate($image, mt_rand(255, 255), mt_rand(255, 255), mt_rand(255, 255));

        $direction = array_rand(array('horizontal', 'vertical',));

        if ( mt_rand(0, 100) === 1 )
        {
            $temp = $color1;
            $color1 = $color2;
            $color2 = $temp;
        }

        $color1 = imagecolorsforindex($image, $color1);
        $color2 = imagecolorsforindex($image, $color2);

        $steps = ( $direction === 'horizontal' ) ? $width : $height;

        $r1 = ( $color1['red'] - $color2['red'] ) / $steps;
        $g1 = ( $color1['green'] - $color2['green'] ) / $steps;
        $b1 = ( $color1['blue'] - $color2['blue'] ) / $steps;

        if ( $direction === 'horizontal' )
        {
            $x1 =& $i;
            $y1 = 0;
            $x2 =& $i;
            $y2 = $height;
        }
        else
        {
            $x1 = 0;
            $y1 =& $i;
            $x2 = $width;
            $y2 =& $i;
        }

        for ( $i = 0; $i <= $steps; $i++ )
        {
            $r2 = $color1['red'] - floor($i * $r1);
            $g2 = $color1['green'] - floor($i * $g1);
            $b2 = $color1['blue'] - floor($i * $b1);
            $color = imagecolorallocate($image, $r2, $g2, $b2);

            imageline($image, $x1, $y1, $x2, $y2, $color);
        }

        // add a few random lines
        for ( $i = 0, $count = mt_rand(5, 50); $i < $count; $i++ )
        {
            $color = imagecolorallocatealpha($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
            imageline($image, mt_rand(0, $width), 0, mt_rand(0, $width), $height, $color);
        }

        // calculate character font-size and spacing
        $default_size = min($width, $height * 2) / strlen($magic_number);
        $spacing = (int) ($width * 0.9 / strlen($magic_number));

        // draw each captcha character with varying attributes
        for ( $i = 0, $strlen = strlen($magic_number); $i < $strlen; $i++ )
        {
            $font = "../fonts/expressions-soul.ttf";

            $color = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
            $angle = mt_rand(-70, $height);

            $size = $default_size / 5 * mt_rand(8, 12);
            $box = imageftbbox($size, $angle, $font, $magic_number[$i]);

            $x = $spacing / 3 + $i * $spacing;
            $y = $height / 2 + ($box[2] - $box[5]) / 4;

            imagefttext($image, $size, $angle, $x, $y, $color, $font, $magic_number[$i]);
        }

        // display image
        header("Content-type: image/jpeg");
        header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header("Connection: close");
        imagejpeg($image);
        imagedestroy($image);
    }
}
// End function


?>

只是寻找让计算机难以阅读的方法的建议

0 个答案:

没有答案