Solution to "the image cannot be displayed due to its own mistakes" when PHP generates the verification code

Source: Internet
Author: User
The following is a detailed analysis of the solution "the image cannot be displayed because of its own mistakes" when PHP generates the verification code. If you need a friend, refer

The following is a detailed analysis of the solution "the image cannot be displayed because of its own mistakes" when PHP generates the verification code. If you need a friend, refer

Today, I used code to implement the PHP verification code generation function, and found that firefox always prompts "image ....... due to its own error and inability to display, the author also prompted that "if the browser shows" image XXX cannot be displayed due to its own error ", try to remove spaces in the text", website space, however, if all spaces in the Code are removed, the Inspection image cannot be displayed.

As a result, most of the solutions for google are

CheckNum_session.php

The Code is as follows:


Header ("Content-type: image/png ");
/*
* Initialization
*/
$ Border = 1; // whether the border is required; 1: 0: not required
$ How = 4; // number of digits of the Verification Code
$ W = $ how * 15; // Image Width
$ H = 20; // Image Height
$ Fontsize = 6; // font size
$ Alpha = "abcdefghijkmnopqrstuvwxyz"; // Verification Code content 1: letter
$ Number = "023456789"; // Verification Code content 2: number
$ Randcode = ""; // verification code string Initialization
Srand (double) microtime () * 1000000); // initialize the random number Seed
$ Im = ImageCreate ($ w, $ h); // create a verification Image
/*
* Draw the Basic Framework
*/
$ Bgcolor = ImageColorAllocate ($ im, 255,255,255); // you can specify the background color.
ImageFill ($ im, 0, 0, $ bgcolor); // fill the background color
If ($ border)
{
$ Black = ImageColorAllocate ($ im, 0, 0, 0); // you can specify the border color.
ImageRectangle ($ im, 0, 0, $ W-1, $ h-1, $ black); // draw a border
}
/*
* Generate random characters by bit
*/
For ($ I = 0; $ I <$ how; $ I ++)
{
$ Alpha_or_number = mt_rand (0, 1); // letters or numbers
$ Str = $ alpha_or_number? $ Alpha: $ number;
$ Which = mt_rand (0, strlen ($ str)-1); // specify the character
$ Code = substr ($ str, $ which, 1); // obtain the character
$ J =! $ I? 4: $ j + 15; // specifies the position of the painted character.
$ Color3 = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100); // Character Color
ImageChar ($ im, $ fontsize, $ j, 3, $ code, $ color3); // draw characters
$ Randcode. = $ code; // Add a verification code string to a bit by bit
}
// Write the verification code string to the session
Session_start ();
$ _ SESSION ['authnum _ session'] = $ randcode;
/*
* Add interference
*/
For ($ I = 0; $ I <1; $ I ++) // draws the background interference line
{
$ Color1 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // interference line color
ImageArc ($ im, mt_rand (-5, $ w), mt_rand (-5, $ h), mt_rand (20,300), mt_rand (20,200), 55, 44, $ color1); // interference line
}
For ($ I = 0; $ I <$ how * 40; $ I ++) // draws background interference points
{
$ Color2 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // interference point color
ImageSetPixel ($ im, mt_rand (0, $ w), mt_rand (0, $ h), $ color2); // interference point
}
// Key code to prevent the problem that 'images cannot be displayed due to their own mistakes .'
Ob_clean ();
/* Drawing ends */
Imagegif ($ im );
ImageDestroy ($ im );
/* Drawing ends */


The confirmation code calls the sessionValidate. php file.

The Code is as follows:


Session_start ();
// Enable the session first at the top,
// Error_reporting (2047 );
Session_destroy ();
// Remove the session to get the new session value every time;
// Use seesion for good results and convenience
?>


Session image verification instance


This example is a session verification instance.

// Print the previous session;
Echo "previous session:". $ _ SESSION [" authnum_session "]."
";
$ Validate = "";
If (isset ($ _ POST ["validate"]) {
$ Validate = $ _ POST ["validate"];
Echo "you just entered:". $ _ POST ["validate"]."
Status :";
If ($ validate! = $ _ SESSION ["authnum_session"]) {
// Determine whether the session value is consistent with the verification code entered by the user;
Echo "incorrect input ";
} Else {
Echo "verified ";
}
}
/*
// Print all sessions;
PrintArr ($ _ SESSION );
Function PrintArr ($ aArray ){
Echo'

'; &lt;BR&gt; print_r ($ aArray); &lt;BR&gt; echo' ';
}
*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.