1<?PHP2 /*********************************3 * * * Function: Implement verification code of the background PHP program * *4 * * * Time: May 14, 2015 ***********5 *********************************/6 7 //session_start () must be at the top of the script, multi-server situation, need to centrally manage session information8 Session_Start();9 Ten One $image= Imagecreatetruecolor (100,30);//Create a true color image, default to black A $bgcolor= Imagecolorallocate ($image, 255,255,255);//assigns a color to a pair of images, returns a color -Imagefill ($image, 0, 0,$bgcolor);//Perform a color area fill at coordinates x and y - the //Note content is a verification code that displays only numbers. Control font size and distribution to avoid overlapping or incomplete display of fonts. - /*For ($i = 0; $i < 4; $i + +) { - $fontsize = 6; - $fontcolor = Imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120)); + $fontcontent = rand (0,9); - + $x = ($i * 100/4) + rand (5,10); A $y = rand (5,10); at - imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); - } */ - - $captcha _code= ";//A variable that is used to place the number in the verification code - for($i= 0;$i< 4;$i++){ in $fontsize= 6; - $fontcolor= Imagecolorallocate ($image,Rand(0,120),Rand(0,120),Rand(0,120)); to + $data= ' abcdefghijkmnpqrstuvwxy3456789 '; - //selects a string of length starting with start from a string the $fontcontent=substr($data,Rand(0,strlen($data)), 1); * $ //let the character appear in the middle of a random placePanax Notoginseng $x= ($i* 100/4) +Rand(5,10); - $y=Rand(5,10); the + //draws a fontcolor character at x, y in the size of the fontsize on image images A $captcha _code= Imagestring ($image,$fontsize,$x,$y,$fontcontent,$fontcolor); the } + $_session[' authcode '] =$captcha _code;//Saving validation information to a hyper-variable array - $ //Increase interference point $ for($i= 0;$i<200;$i++){ - $pointcolor= Imagecolorallocate ($image,Rand(50,200),Rand(50,200),Rand(50,200)); - the //draw a point in the image images with a color of one pixel in the position of X and Y -Imagesetpixel ($image,Rand(1,99),Rand(1,29),$pointcolor);Wuyi } the - //increase the lines used to disturb Wu //Interference information must control the color, avoid "distracting" - for($i= 0;$i< 3;$i++){ About $linecolor= Imagecolorallocate ($image,Rand(80,220),Rand(80,220),Rand(80,220)); $ - //Add a color-colored line between two points in the image -Imageline ($image,Rand(1,99),Rand(1,29),Rand(1,99),Rand(1,29),$linecolor); - } A + //before you enter a picture, you must output the header information in advance. the //default is black background color - Header(' Content-type:image/png '); $ the the //The imagepng function enters the image in PNG format into a browser or file theImagepng ($image); the - in the //destroying an image when end ends theImagedestroy ($image);
1<?PHP2 /*********************************3 * * * * Function: Implement verification code of the foreground form style *4 * * * Time: May 14, 2015 ***********5 *********************************/6 7 //If there is a $_request[' authcode '] variable, the user enters the verification code8 if(isset($_request[' Authcode '])){9 Session_Start();Ten One //strtolower function makes no case A if(Strtolower($_request[' Authcode ']) ==$_session[' Authcode ']){ - Echo' <font color = ' #0000CC ' > enter correct </font> '; -}Else{ the Echo' <font color = ' #0000CC ' ><b> input error </b></font> '; - } - Exit(); - } +?> - +<! DOCTYPE html> A at - -<meta charset = "Utf-8"/> -<title> Confirm Verification Code </title> - -<body> in<!--Pass the user-entered information to yourself via the Post method -<form method = "POST" action = "./form.php" > to<p>Verification Code Picture: + "width = =" "/> -<!--to achieve dynamic verification: with the JS picker to verify the code picture, with JS to modify the verification Code image address (change src)-- the<a href = "javascript:void (0)" onclick = "document.getElementById (' captcha_img ')." src = './captcha.php?r= ' + Math.random () "> Change one?" </a> *</p> $<p>Panax Notoginseng Please enter the contents of the Verification code: -<input type = "text" name = "Authcode" value = ""/> the</p> +<p> A<input type = "submit" value = "commit" style = "padding:6px 20px;" /> the</p> +</form> -</body> $
Core Technical Analysis:
1. Generate Basemap
2. Generate Verification Code content
3. Generate the Verification code picture
4. Verification Code Content
PHP Implementation Verification Code