//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);
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
<?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);
?>
After changing it to this, it will become 3 numbers...