1. According to the GD Library in PHPPicture to process, draw a picture of the verification code, code.php
2. Form interface, simple session save and compare with user input, determine whether the validation is correct, form.php
<?PHP Session_Start();//must be at the top of the script $image=imagecreatetruecolor (100, 30);//Imagecreatetruecolor function to build a true color image//Generate color pixels $bgcolor=imagecolorallocate ($image, 255, 255, 255);//White background imagecolorallocate function assigns color to an image $textcolor=imagecolorallocate ($image, 0,0,255);//Blue text//fill function, XY determines coordinates, color color performs area fill colorImagefill ($image, 0, 0,$bgcolor); $captch _code="";//Initial empty value//the loop, loop fetch number for($i= 0;$i<4;$i++){ $fontsize=6; $x=($i*25) +Rand(5,10); $y=Rand(5,10);//position random//$fontcontent = $i >2?chr (rand (97,122)): Chr (rand (65,90));//is lowercase, otherwise uppercase $data= ' abcdefghijkmnpqrstuvwxyz3456789 '; $fontcontent=substr($data,Rand(0,strlen($data)-1), 1);//strlen is just a counter work with numbers and letters of verification code//can be understood for an array length of 0 to
$fontcolor=imagecolorallocate($image,Rand(0, -),Rand( 0,+),rand(0 ,+)); ///random RGB () value can be determined by itself
Imagestring ($image,$fontsize,$x,$y,$fontcontent,$fontcolor);//to draw a line of strings horizontally $captch _code.=$fontcontent;} $_session[' Authcode ']=$captch _code;//Save the variable in the Authcode variable of the session//the loop, cycle the point of the background interference for($m= 0;$m<=600;$m++){ $x 2=Rand(1,99); $y 2=Rand(1,99); $pointcolor=imagecolorallocate ($image,Rand(0,255),Rand(0,255),Rand(0,255)); Imagesetpixel ($image,$x 2,$y 2,$pointcolor);//to draw a sequence of dots horizontally } //the loop, the loop draws the interfering line for($i= 0;$i<=10;$i++){ $x 1=Rand(0,99); $y 1=Rand(0,99); $x 2=Rand(0,99); $y 2=Rand(0,99); $linecolor=imagecolorallocate ($image,Rand(0,255),Rand(0,255),Rand(0,255)); Imageline ($image,$x 1,$y 1,$x 2,$y 2,$linecolor);//Draw a line segment } Header(' Content-type:image/png '); Imagepng ($image); //destroyedImagedestroy ($image);?>
<-------------------------------- code.php---------------------------End----------------------------------------->
<?PHP//session save value and match user input value if(isset($_request[' Authcode '])) { Session_Start(); if(Strtolower($_request[' Authcode ']) ==$_session[' Authcode ']) {//Strtolower converted to lowercase functions Echo"Input correct!" "; #code ... } Else{ Echo"Input Error! "; } Exit(); }? ><! DOCTYPE html>
<---------------------------------form.php ---------------End of form interface---------------------------------------------->
PHP implements a simple captcha function