The production of character verification code is divided into five steps:
1. Generating images
2. Generate Random Numbers
3. Making interference points, lines
4. Output image
5. Destroying images
Directly below the code
Server-side:
<?phpsession_start (); $width = n; $length = n; $captch _code = "; $image = Imagecreatetruecolor ($width, $length); $ color = imagecolorallocate ($image, 255, 255, 255), Imagefill ($image, 0, 0, $color);//Generate random number for ($i =0; $i <4; $i + +) {$ FontSize = 6; ? $fontcolor = Imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120)); $data = " abcdefghijklmnopqrstuvwxyz1234567890 "; $fontcontent = substr ($data, Rand (0,strlen ($DATA)), 1); $captch _code. = $ fontcontent; $x = ($i * $width/4) + rand ($length/6, $length/3); $y = rand ($length/6, $length/3); Imagestring ($image, $ FontSize, $x, $y, $fontcontent, $fontcolor);} $_session[' authcode ' = $captch _code;//interference point for ($i =0; $i <200; $i + +) {$pointcolor = Imagecolorallocate ($image, Rand ( 50,200), Rand (50,200), Rand (50,200)), Imagesetpixel ($image, rand (1, $width-1), rand (1, $width-1), $pointcolor);} Interference line for ($i =0; $i <5; $i + +) {$linecolor = Imagecolorallocate ($image, Rand (80,220), Rand (80,220), Rand (80,220)); Imageline ($image, rand (1, $width-1), rand (1, $width/3-1), rand (1,$width-1), rand (1, $width/3-1), $linecolor);} Header ("Content-type:image/png"); Imagepng ($image); Imagedestroy ($image);
Client validation Code:
<?phpif (Isset ($_request[' Authcode ')) {session_start (); if (Strtolower ($_request[' authcode ']) = = $_session[' Authcode '] {echo ' <font color= "#0000cc" > Input correct </font> ';} Else{echo ' <font color= "#cc0000" ><b> input error </b></font> ';} Exit ();}? ><! DOCTYPE html>
Result diagram:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
PHP Verification code--character