PHP验证码不起作用


PHP Captcha didnt work

我有问题与我的验证码方法,字符代码不工作。

这是我到目前为止的代码:

<?php
    class captcha 
    {
        function genCaptcha()
        {
            $temp;
            $tempCap = "";
            for ($i=1; $i <= 6 ; $i++) 
            { 
                if ($i%2 == 1) $temp = floor(rand()*10);
                else $temp = chr(floor(rand()*26)+65);
                $tempCap = $tempCap + $temp;
            }
            echo $tempCap;
        }
    }
    $asd = new captcha();
    $asddd = $asd->genCaptcha();
?>

它应该像这样回显4A7D0Z(刷新页面时将显示随机验证码)。我该如何做到这一点?

这一行:

$tempCap = $tempCap + $temp;

您正在尝试将字符串添加在一起。在PHP中,将字符串加在一起使用句号而不是+号。

$tempCap = $tempCap . $temp;

试试这个,这将允许您调整要连接的值的权重。

这种路由的好处是可以省略可能产生问题的字符——例如0到0、I到1到1等等。

<?php
    class captcha 
    {
        /*
         * These should total 100
         */
        private $weightNumber = 44;
        private $weightUpper = 20;
        private $weightLower = 36;
        public $tempcap;
        private $capLen = 6;
        private $lower  = 'abcdefghijklmnopqrstuvwxyz';
        private $upper  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        private $number = '0123456789';

        function genCaptcha()
        {
            $tempCap = "";
            $blocks = array();
            /*
             * These blocks are multiplied by numbers to equal 260 characters, to provide even weighting.
             */
            $blocks[] = str_repeat($this->lower, $this->weightLower * 10);  // sum 260
            $blocks[] = str_repeat($this->upper, $this->weightUpper * 10);  // sum 260
            $blocks[] = str_repeat($this->number, $this->weightNumber * 26);  // sum 260
            /*
             * Strings are joined, and then split into an array of individual characters.
             */
            $seed = str_split( implode('', $blocks) );

            /*
             * Loop over the number of items in the captcha block
             */
            for ($i = 0; $i < $this->capLen; $i++) {
                /*
                 * Concatenate a random element from the 'shuffled' array using `mt_rand()`, a better random generator in PHP
                 */ 
                shuffle($seed);
                $rkey     = mt_rand(1,count($seed));
                $tempCap .= $seed[$rkey];
                unset($seed[$rkey]);
            }
            /* 
             * Echo the generated CAPTCHA, probably better to do this as a returned var
             */
            $this->tempcap = implode('', $seed);
            return $tempCap;
        }
    }
    $asd = new captcha();
    // Here you could (if you chose to return from class method), do `echo $asd->genCaptcha();`
    $asddd = $asd->genCaptcha();
    echo $asddd;

    preg_match_all('([a-z]{1}+)', $asddd, $lowermatches);
    preg_match_all('([A-Z]{1}+)', $asddd, $uppermatches);
    preg_match_all('([0-9]{1}+)', $asddd, $numbermatches);
    echo '<br/>';echo '<br/>';echo '<br/>';
    echo 'Lowercase ('.count($lowermatches[0]).'): ' . round(100 * count($lowermatches[0])/strlen($asddd),1) . '%';
    echo '<br/>';
    echo 'Uppercase ('.count($uppermatches[0]).'): ' . round(100 * count($uppermatches[0])/strlen($asddd),1) . '%';
    echo '<br/>';
    echo 'Numerics ('.count($numbermatches[0]).'): ' . round(100 * count($numbermatches[0])/strlen($asddd),1) . '%';

    preg_match_all('([a-z]{1}+)', $asd->tempcap, $lowermatches);
    preg_match_all('([A-Z]{1}+)', $asd->tempcap, $uppermatches);
    preg_match_all('([0-9]{1}+)', $asd->tempcap, $numbermatches);
    echo '<br/>';echo '<br/>';echo '<br/>';
// var_dump($matches[0]);
    echo 'Lowercase ('.count($lowermatches[0]).'): ' . round(100 * count($lowermatches[0])/strlen($asd->tempcap),1) . '%';
    echo '<br/>';
    echo 'Uppercase ('.count($uppermatches[0]).'): ' . round(100 * count($uppermatches[0])/strlen($asd->tempcap),1) . '%';
    echo '<br/>';
    echo 'Numerics ('.count($numbermatches[0]).'): ' . round(100 * count($numbermatches[0])/strlen($asd->tempcap),1) . '%';
 ?>

jzt76W
Lowercase (3): 50%
Uppercase (1): 16.7%
Numerics (2): 33.3%
Lowercase (9357): 36%
Uppercase (5199): 20%
Numerics (11438): 44%

剩下的就看你了!

查看这里的示例,演示了来自示例字符串的数字百分比:

http://codepad.viper - 7. - com/hip3in