PHP Captcha没有用

时间:2014-06-01 17:58:54

标签: php captcha

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

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

<?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回显(当你刷新页面时会显示一个随机的验证码)。我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

在这一行:

$tempCap = $tempCap + $temp;

您正在尝试将字符串添加到一起。在PHP中添加字符串是使用句点而不是+ -sign完成的。

$tempCap = $tempCap . $temp;

这应该是正确的。

答案 1 :(得分:0)

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

<强>被修改

此路线的好处是,您可以省略可能造成问题的字符 - 例如0到O,I到l到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