Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 验证码修改六个字母_Php_Captcha_Letters - Fatal编程技术网

Php 验证码修改六个字母

Php 验证码修改六个字母,php,captcha,letters,Php,Captcha,Letters,我有以下验证码: <?php class CaptchaView extends View { private $fontsDir = 'captcha_resources/fonts/'; private $backgroundsDir = 'captcha_resources/backgrounds/'; private static $dirCache = array(); public function render($controller)

我有以下验证码:

<?php

class CaptchaView extends View
{
    private $fontsDir = 'captcha_resources/fonts/';
    private $backgroundsDir = 'captcha_resources/backgrounds/';
    private static $dirCache = array();

    public function render($controller)
    {
        $code = $controller->viewVars["code"];

        /**
         * The next part is orginnaly written by ted from mastercode.nl and modified for using in this mod.
         **/

        header("content-type:image/png");
        header('Cache-control: no-cache, no-store');

        $width = 100;
        $height = 30;

        $img = imagecreatefrompng(self::backgroundImage());

        // add noise
        for ($i = 0; $i < 1; $i++) {
            $horizontal_progress = 0;
            $vertical_pos = rand(1, $height / 2);
            do {
                $horizontal_step_size = floor(rand(1, $width / 5));

                imageline($img, $horizontal_progress, $vertical_pos, ($horizontal_progress += $horizontal_step_size), ($vertical_pos = rand(1, $height)), self::color("tekst"));
            } while ($horizontal_progress < $width);
        }

        $background = imagecolorallocate($img, self::color("bg"), self::color("bg"), self::color("bg"));

        for ($g = 0; $g < 30; $g++) {
            $t = rand(10, 20);
            $t = $t[0];

            $ypos = rand(0, $height);
            $xpos = rand(0, $width);

            $kleur = imagecolorallocate($img, self::color("bgtekst"), self::color("bgtekst"), self::color("bgtekst"));

            imagettftext($img, self::size(), self::move(), $xpos, $ypos, $kleur, self::font(), $t);
        }

        $stukje = $width / (strlen($code) + 3) + 5;

        for ($j = 0; $j < strlen($code); $j++) {
            $tek = $code[$j];
            $ypos = rand(23, 27);
            $xpos = $stukje * ($j + 1) - 5;

            $color2 = imagecolorallocate($img, self::color("tekst"), self::color("tekst"), self::color("tekst"));

            imagettftext($img, self::size(), self::move(), $xpos, $ypos, $color2, self::font(), $tek);
        }

        imagepng($img);
        imagedestroy($img);
    }
    /**
     * Some functions :)
     * Also orginally written by mastercode.nl
     **/

    /**
     * Function to create a random color
     * @auteur mastercode.nl
     * @param $type string Mode for the color
     * @return int
     **/
    private static function color($type)
    {
        switch ($type) {
            case "bg":
                $color = rand(224, 255);
                break;

            case "tekst":
                $color = rand(0, 127);
                break;

            case "bgtekst":
                $color = rand(200, 224);
                break;

            default:
                $color = rand(0, 255);
                break;
        }
        return $color;
    }

    /**
     * Function to ranom the size
     * @auteur mastercode.nl
     * @return int
     **/
    private static function size()
    {
        return rand(18, 22);
    }

    /**
     * Function to random the posistion
     * @auteur mastercode.nl
     * @return int
     **/
    private static function move()
    {
        return rand(-22, 22);
    }

    /**
     * Function to return a ttf file from fonts map
     * @auteur mastercode.nl
     * @return string
     **/
    function randomFileByExt($dir, $ext)
    {
        $f = opendir($dir);

        if (empty(self::$dirCache[$dir])) {
            $ar = array();
            while (($file = @readdir($f)) !== false) {
                if (!in_array($file, array('.', '..')) && substr_compare($file, $ext, -strlen($ext)) == 0) {
                    $ar[] = $file;
                }
            }

            self::$dirCache[$dir] = $ar;
        } else {
            $ar = self::$dirCache[$dir];
        }

        if (count($ar)) {
            $i = rand(0, (count($ar) - 1));
            return $dir . $ar[$i];
        }
    }

    private function backgroundImage()
    {
        return self::randomFileByExt(Config::get("VIEWS_PATH") . $this->backgroundsDir, ".png");
    }

    private function font()
    {
        return self::randomFileByExt(Config::get("VIEWS_PATH") . $this->fontsDir, ".ttf");
    }
}
1)最佳解决方案是实现现有的Captcha,如reCaptcha等

  • 文件:
  • PHP:
2) 要将代码修改为按6而不是4个字母,请编辑代码的以下部分:

$stukje = $width / (strlen($code) + 5) + 5; //instead of  "+3) +5;"

应该可以,但我不能测试它,因为我不知道它在哪个mod中使用(什么是CMS,PHP框架,…)

如果bot可以识别4个字母,它也可以识别6个。如果它是一个人而不是一个程序,那么它将能够识别6而不会出现问题。同意@zerkms。如果机器人能够阅读这4个字母。那么验证码可能是错的。尝试使用现有的captcha,如reCaptcha。我必须使用另一个captcha代码进行更改?问题是,captcha的页面位于smarty tpl文件中。对我来说,不只是用reCaptcha替换此captcha。。。不过,谢谢你的回答