A workaround for PHP when generating a verification code "the image cannot be displayed because of its own error" _php

Source: Internet
Author: User
Keywords PHP Verification code
Tags border color set background
Today, the implementation of PHP generated check code using code, found that Firefox has been prompted "image ...." Because of its own mistakes can not be displayed, the author also suggested that "if the browser display" image xxx because of its own error can not be displayed ", may try to remove the text hollow lattice", but the code of all the blanks are removed or can not display the inspection picture.

So in depth Google, most of the solutions will also be "

Confirmation code generation file checknum_session.php
Copy CodeThe code is as follows:
Header ("Content-type:image/png");
/*
* Initialization
*/
$border = 1; Whether to border 1 to: 0 do not
$how = 4; Verify number of code bits
$w = $how *15; Picture width
$h = 20; Picture height
$fontsize = 6; Font size
$alpha = "ABCDEFGHIJKMNOPQRSTUVWXYZ"; Verification Code Content 1: Letters
= "023456789"; Verification Code Content 2: Digital
$randcode = ""; Validation code string initialization
Srand (Double) microtime () *1000000); Initialize random number Seed
$im = Imagecreate ($w, $h); Create a validation picture
/*
* Draw the basic frame
*/
$bgcolor = Imagecolorallocate ($im, 255, 255, 255); Set Background color
Imagefill ($im, 0, 0, $bgcolor); Fill background color
if ($border)
{
$black = imagecolorallocate ($im, 0, 0, 0); Set Border color
Imagerectangle ($im, 0, 0, $w-1, $h-1, $black);//Draw Border
}
/*
* Random character generation by bit
*/
for ($i =0; $i < $how; $i + +)
{
$alpha _or_number = Mt_rand (0, 1); Letters or numbers?
$str = $alpha _or_number? $alpha:;
$which = Mt_rand (0, strlen ($str)-1); which character to take
$code = substr ($str, $which, 1); Take character
$j =! $i? 4: $j +15; Draw character position
$color 3 = imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100)); Character Color
Imagechar ($im, $fontsize, $j, 3, $code, $color 3); Paint characters
$randcode. = $code; Add a CAPTCHA string to a bit
}
Write the CAPTCHA string to the session
Session_Start ();
$_session[' authnum_session '] = $randcode;
/*
* Add interference
*/
for ($i =0; $i <1; $i + +)//Draw background interference line
{
$color 1 = 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), $color 1); Interference Line
}
for ($i =0; $i < $how *40; $i + +)//plot background interference points
{
$color 2 = 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), $color 2); Interference Point
}
Critical code to prevent ' images from appearing ' because of their own mistakes
Ob_clean ();
/* End of drawing */
Imagegif ($im);
Imagedestroy ($im);
/* End of drawing */

ACK Code call file sessionvalidate.php
Copy CodeThe code is as follows:
session_start ();
At the beginning of the page to open the session,
Error_reporting (2047);
Session_destroy ();
Remove the session to get a new session value each time;
With seesion effect is good, also very convenient
?>


Session Picture Verification Example


This example validates an instance of the session

Print the previous session;
echo "Last SESSION:". $_session["Authnum_session"]. "
";
$validate = "";
if (Isset ($_post["Validate")) {
$validate =$_post["Validate"];
echo "You have just entered:". $_post["Validate"]. "
Status: ";
if ($validate!=$_session["Authnum_session"]) {
Determine if the session value is consistent with the user input verification code;
echo "Incorrect input";
}else{
echo "Pass verification";
}
}
/*
Print all session;
Printarr ($_session);
function Printarr ($aArray) {
Echo '

&lt;xmp&gt;'; &amp;lt;br&amp;gt;print_r ($aArray); &amp;lt;br&amp;gt;echo '&lt;/xmp&gt; ';
}
*/
?>
  • 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.