The PHP verification code image cannot be output. The PHP verification code Image cannot output the file name global_func.phpfunction_code () {number of running verification codes $ _ rnd_code4; for ($ i0; $ I $ _ rnd_code; $ I ++) {$ _ nmsg. dechex (mt_rand (0, PHP verification code picture cannot be output
File name global_func.php
Function _ code (){
// Run the verification code
// Number of note codes
$ _ Rnd_code = 4;
For ($ I = 0; $ I <$ _ rnd_code; $ I ++ ){
$ _ Nmsg. = dechex (mt_rand (0, 15 ));
}
// Save it in session
$ _ SESSION ['code'] =$ _ nmsg;
// 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 );
$ _ Flag = false;
If ($ _ flag ){
// Black, border
$ _ Black = imagecolorallocate ($ _ img, 0, 0 );
Imagerectangle ($ _ img, 0, 0, $ _ width-1, $ _ height-1, $ _ black );
}
// Draw 6 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, mt_rand (0, $ _ width), mt_rand (0, $ _ height), mt_rand (0, $ _ width), mt_rand (0, $ _ height), $ _ rnd_color );
}
// Snowflake
For ($ I; I I <100; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ _ img, 1, mt_rand (1, $ _ width), mt_rand (1, $ _ height), '*', $ _ rnd_color );
}
// Output the verification code
For ($ I = 0; $ I
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200 ));
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], $ _ rnd_color );
}
// Output image
Header ('content-Type: image/png ');
Imagepng ($ _ img );
// Destroy
Imagedestroy ($ _ img );
}
If I call function _ code () in the global_func.php file, the error is a Red Cross.
Code. php
_ Code ();
This is something I can use directly in code. php.
Code. php
// Number of note codes
$ _ Rnd_code = 4;
For ($ I = 0; $ I <$ _ rnd_code; $ I ++ ){
$ _ Nmsg. = dechex (mt_rand (0, 15 ));
}
// Save it in session
$ _ SESSION ['code'] =$ _ nmsg;
// 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 );
$ _ Flag = false;
If ($ _ flag ){
// Black, border
$ _ Black = imagecolorallocate ($ _ img, 0, 0 );
Imagerectangle ($ _ img, 0, 0, $ _ width-1, $ _ height-1, $ _ black );
}
// Draw 6 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, mt_rand (0, $ _ width), mt_rand (0, $ _ height), mt_rand (0, $ _ width), mt_rand (0, $ _ height), $ _ rnd_color );
}
// Snowflake
For ($ I; I I <100; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ _ img, 1, mt_rand (1, $ _ width), mt_rand (1, $ _ height), '*', $ _ rnd_color );
}
// Output the verification code
For ($ I = 0; $ I
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200 ));
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], $ _ rnd_color );
}
// Output image
Header ('content-Type: image/png ');
Imagepng ($ _ img );
// Destroy
Imagedestroy ($ _ img );
This verification code is not encapsulated into a function. you can use the verification code.
However, after using function _ code (), I will display a red cross in html when calling _ code. I don't know why
------ Solution --------------------
Will it be the reason for header ('content-Type: image/png,
The BOM header affects the browser's expected file type.
------ Solution --------------------
Even if it is written as a function, the call is fine.
Function _ code (){
// Run the verification code
}
?>
Another page
Include ("./test. php ");
_ Code ();
------ Solution --------------------
You will know why when you encounter an error in php ..
Pipeline file name global_func.php function _ code () {// run the verification code // The number of note codes $ _ rnd_code = 4; for ($ I = 0; $ I $ _ rnd_code; $ I ++) {$ _ nmsg. = dechex (mt_rand (0 ,...