Verification code question session_start ();
// Number of random codes
$ _ Rnd_code = 4;
// Create a random code
For ($ I = 0; $ I <$ _ rnd_code; $ I ++ ){
$ _ Nmsg. = dechex (mt_rand (0, 15 ));
}
// Save it in the session
$ _ SESSION ['code'] =$ _ nmsg;
// Set the length and height
$ _ Width = 75;
$ _ Height = 25;
// Create an image
$ _ Img = imagecreatetruecolor ($ _ width, $ _ height );
// White
$ _ White = imagecolorallocate ($ _ img, 255,255,255 );
// Fill
Imagefill ($ _ img, 0, 0, $ _ white );
// Black border
$ _ Black = imagecolorallocate ($ _ img, 0, 0 );
Imagerectangle ($ _ img, 0, 0, $ _ width-1, $ _ height-1, $ _ black );
// Random six lines
For ($ I = 0; $ I <6; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imageline ($ _ img, rand (0, $ _ width), rand (0, $ _ height), rand (0, $ _ width), rand (0, $ _ height), $ _ rnd_color );
}
// Output the verification code
For ($ I = 0; $ I Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] ['$ I'], $ _ black );
}
The four characters output from a verification code result are all the same. let's take a look at what's going on.
Reply to discussion (solution)
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] ['$ I'], $ _ black );
This should be the case
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], $ _ black );
(! ) Notice: Undefined variable: _ nmsg in \ demo. php on line 10
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0
(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0
(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0
(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0
(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0
Change it to this.
Thank you for making the money.