我的验证码方法有问题,字符代码不起作用。
到目前为止,这是我的代码:
<?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
这样回4A7D0Z
(刷新页面时,它将显示随机的验证码)。 我该如何完成?
在这行上:
$tempCap = $tempCap + $temp;
您正在尝试将字符串添加在一起。 在PHP中将字符串添加在一起是用句点而不是+号完成的。
$tempCap = $tempCap . $temp;
这应该是正确的。
尝试此操作,这将允许您调整要合并的值的权重。
编辑
这种方法的好处是,您可以省略可能引起问题的字符-例如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%
其余的取决于您!
更新
请参阅此处的示例,其中包含示例字符串中数字百分比的演示: