PHP to generate the code when the "image because of its own error can not display" solution _php skills

Source: Internet
Author: User
Tags border color set background

Today, the use of code to generate a check code PHP implementation, found that Firefox has always been prompted "image .... Because of its own fault can not show the problem, the author also suggested that "if the browser display" image xxx because of its own error can not be displayed, as far as possible to remove the text hollow ", but the code in all the blanks are removed or can not show the inspection picture.

So deep Google, most of the solutions will be " This code before the space carriage return everything deleted to prevent the HTML output, but this does not solve my problem, and later saw someone in the header ("Content-type:image/png"), the code before the use of Ob_clean () to clear the output, The result was a successful attempt. The original code is modified to post to the following for a rainy hour:

Confirmation code generation file checknum_session.php

Copy Code code as follows:

Header ("Content-type:image/png");
/*
* Initialization
*/
$border = 1; Do you want to have a border 1:0 don't
$how = 4; Verify code digits
$w = $how *15; Picture width
$h = 20; Picture height
$fontsize = 6; Font size
$alpha = "ABCDEFGHIJKMNOPQRSTUVWXYZ"; CAPTCHA content 1: Letters
$number = "023456789"; Authenticode Content 2: Numbers
$randcode = ""; Authentication Code String Initialization
Srand (Double) microtime () *1000000); Initialize random number of seeds
$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
}
/*
* Bitwise random Character generation
*/
for ($i =0; $i < $how; $i + +)
{
$alpha _or_number = Mt_rand (0, 1); A letter or a number
$str = $alpha _or_number? $alpha: $number;
$which = Mt_rand (0, strlen ($str)-1); which character to take
$code = substr ($str, $which, 1); Take characters
$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 immediately color
Imagechar ($im, $fontsize, $j, 3, $code, $color 3); Painting characters
$randcode. = $code; Step by step to add a verification code string
}
Writes a validation code 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); Jamming line
}
for ($i =0; $i < $how *40; $i + +)//Draw 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); Jamming Point
}
Critical code to prevent problems with ' images that cannot be displayed because of their own errors '
Ob_clean ();
/* Drawing End * *
Imagegif ($im);
Imagedestroy ($im);
/* Drawing End * *

Confirmation code call file sessionvalidate.php
Copy Code code as follows:

<span style= "FONT-SIZE:14PX;" ><?php
Session_Start ();
In the page first to open session,
Error_reporting (2047);
Session_destroy ();
The session is removed so that a new session value can be taken each time;
With seesion effect is good, also very convenient
?>
<title>session Picture Validation Example </title>
<body>
This example is the session validation instance
<form action= "" method= "POST" >
Verify code: <input type= "text" name= "Validate" value= "" Size=10> <br>
<input type= "Submit" >
</form>
<?php
Print the previous session;
echo "Prev session:<b>". $_session["Authnum_session". " </b><br> ";
$validate = "";
if (Isset ($_post["validate"])) {
$validate =$_post["Validate"];
echo "You just entered is:". $_post["Validate"]. " <br> Status: ";
if ($validate!=$_session["Authnum_session"]) {
To determine whether the session value is consistent with the user's input validation code;
echo "<font color=red> input error </font>";
}else{
echo "<font color=green> through validation </font>";
}
}
/*
Print all sessions;
Printarr ($_session);
function Printarr ($aArray) {
Echo ' <xmp> ';
Print_r ($aArray);
Echo ' </xmp> ';
}
*/
?></span>

Related Article

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.