Why does my verification code only show one number?
丫头
丫头 2018-04-21 11:01:31
0
5
3042

//My code for this lesson is like this. I checked it several times and didn’t see any errors, but the verification code displayed only has one number...Why is this happening? Please give me some guidance


<?php

//Background for generating verification code

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

//The size of the background image

$width=60;

$height=15;

//Create the canvas

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

//Assign color

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

// Fill the color into the canvas

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

//Generate the value of the verification code

$chars='1234567890';

$chars_len=strlen($chars);

$code_len=4;//Verification code length

$code="";//Initial value

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

$rand=mt_rand(0,$chars_len-1);//Randomly take out four numbers

$code=$rand;//Connect the extracted numbers together

}

//Save it into the session and use verification

session_start();

$_SESSION['ver_code']=$code;

//Randomly assign string color

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

//Calculate the string to be displayed in the center

//The size of the string

$font=5;

//Canvas size

$img_w=imagesx($img);

$img_h=imagesy($img);

//Font size

$font_w=imagefontwidth($font);

$font_h=imagefontheight($font);

//String size

$code_w=$font_w*$code_len ;

$code_h=$font_h;

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

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

//Output the verification code to the canvas

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

/ /Direct output

imagejpeg($img);

imagedestroy($img);


##?>


丫头
丫头

好好学PHP。

reply all(3)
王承毅

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

$rand=mt_rand(0,$chars_len-1);//Randomly take out numbers

$code.=$rand; //Splice the extracted four digits together

}

You are missing a connector

  • reply That's ok, thank you
    丫头 author 2018-04-22 10:53:02
丫头

<?php

//Background for generating verification code

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

//Background image Size

$width=60;

$height=15;

//Create canvas

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

//Assign color

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

//Fill color to canvas

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

//Generate the value of the verification code

$chars='1234567890';

$chars_len=strlen( $chars);

$code_len=4;//Verification code length

$code="";//Initial value

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

$rand=mt_rand(0,$chars_len-1);//Randomly take out four numbers

$code.=$rand ;//Connect the retrieved numbers together

}

//Save it into the session and use verification

session_start();

$_SESSION ['ver_code']=$code;

//Randomly assign string color

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

//Calculate the string to be displayed in the center

//The size of the string

$font=5;

//Canvas size

$img_w=imagesx($img);

$img_h=imagesy($img);

//Font size

$font_w=imagefontwidth($ font);

$font_h=imagefontheight($font);

//String size

$code_w=$font_w*$code_len;

$code_h=$font_h;

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

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

//Output the verification code to the canvas

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

//Output directly

imagejpeg($img);

imagedestroy($img);


?>


丫头

TIM图片20180421112016.png

After changing it to this, it will become 3 numbers...

  • reply There is an error in your for loop, it should be like this. for($i=0;$i&lt;$code_len;$i++){ $rand=mt_rand(0,$chars_len-1);//Randomly take out numbers $code.=$rand; //Splice the four digits taken out together }
    王承毅 author 2018-04-21 12:35:29
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template