This PHP generated CAPTCHA code principle generates a random number--Create picture--random number in the picture--Save in session, look at the process verification code picture generation notification browser will output PNG picture ready random number generator seed srand (double ) Microtime () *1000000); Drawing a four-bit integer verification code into a picture
/php Tutorial Generate CAPTCHA Code
/*=====================================================
This PHP generated CAPTCHA code principle generates a random number--Create picture--random number in the picture--Save in session, look at the process verification code picture generation notification browser will output PNG picture ready random number generator seed srand (double ) Microtime () *1000000); Drawing a four-bit integer verification code into a picture
=======================================================*/
/*=====================
Generating random String functions
=====================*/
function Random ($length) {
$hash = ";
$chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
$max = strlen ($chars)-1;
Mt_srand (Double) microtime () * 1000000);
for ($i = 0; $i < $length; $i + +) {
$hash. = $chars [Mt_rand (0, $max)];
}
return $hash;
}
Verification Code Picture Generation
Session_Start ();
Notifies the browser that a PNG image will be exported
Header ("Content-type:image/png");
Get the random number generator seed ready.
Srand (Double) microtime () *1000000);
Prepare the relevant parameters for the picture
$im = Imagecreate (62,22);
$black = Imagecolorallocate ($im, 0,0,0); RGB Black Identifier
$white = Imagecolorallocate ($im, 255,255,255); RGB White Identifier
$gray = Imagecolorallocate ($im, 179,183,185); RGB Gray Identifiers
Start drawing
Imagefill ($im, 0,0, $gray);
while (($randval =rand ()%100000) <10000); {
$_session["Check_code"] = $randval;
Drawing a four-bit integer verification code into a picture
$randval =random (4);
$_session["Check_code"]= $randval;
Imagestring ($im, 5, 3, $randval, $white);
//}
adding interfering pixels
for ($i =0; $i <150; $i + +) {
$randcolor = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $white);
}
Output validation Picture
Imagepng ($im);
Destroy image identifiers
Imagedestroy ($im);
?>
http://www.bkjia.com/PHPjc/633022.html www.bkjia.com true http://www.bkjia.com/PHPjc/633022.html techarticle This PHP generated CAPTCHA code principle generates a random number--Create picture--random number in the picture--Save in session, look at the process verification code picture generation notification browser will output p ...