为什么我的验证码只显示一个数字?
丫头
丫头 2018-04-21 11:01:31
0
5
2886

//这节课我的代码是这样的,我检查了几遍,没看出有什么错误,但是显示出来的验证码只有一个数字...为什么会这样,求各位大神指导


<?php

//生成验证码的背景

header('Content-type:image/jpeg');

//背景图的尺寸

$width=60;

$height=15;

//创建画布

$img=imagecreatetruecolor($width, $height);

//分配颜色

$white=imagecolorallocate($img, 0xff, 0xff, 0xff);

//填充颜色到画布

imagefill($img, 0, 0, $white);

//生成验证码的值

$chars='1234567890';

$chars_len=strlen($chars);

$code_len=4;//验证码长度

$code="";//初始值

for ($i=1; $i < $code_len; ++$i) { 

$rand=mt_rand(0,$chars_len-1);//随机取出四个数字

$code=$rand;//将取出的数字连接在一起

}

//存入session中,用验证

session_start();

$_SESSION['ver_code']=$code;

//随机分配字符串颜色

$str_color=imagecolorallocate($img, mt_rand(0,255),  mt_rand(0,255),  mt_rand(0,255));

//计算字符串居中显示

//字符串的大小

$font=5;

//画布尺寸

$img_w=imagesx($img);

$img_h=imagesy($img);

//字体尺寸

$font_w=imagefontwidth($font);

$font_h=imagefontheight($font);

//字符串尺寸

$code_w=$font_w*$code_len;

$code_h=$font_h;

$x=($img_w-$code_w)/2;

$y=($img_h-$code_h)/2;

//把验证码输出到画布上

imagestring($img, $font, $x, $y, $code, $str_color);

//直接输出

imagejpeg($img);

imagedestroy($img);


?>


丫头
丫头

好好学PHP。

répondre à tous(3)
丫头

TIM图片20180421112016.png

改成这样之后就会变成3个数字了...

  • répondre 你for循环出错了,应该是这样的。 for($i=0;$i&lt;$code_len;$i++){ $rand=mt_rand(0,$chars_len-1);//随机取出数字 $code.=$rand; //将取出的四位数字拼接在一起 }
    王承毅 auteur 2018-04-21 12:35:29
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!