The php image verification code is first generated on the index. php page.
Function identity () {$ this-> load-> view ('index', $ data);} function codes () {if ($ _ POST ['code'] ==$ _ SESSION ['code']) {echo 'OK';} else {echo 'no ';}} function create_code () {session_start (); // Generate the verification code image ob_clean (); // header ("Content-type: image/png "); // Full number $ str = "1, 2, 4, 5, 6, 7, 8, 9, 0, a, B, c, d, e, f"; // the character to be displayed, you can add or delete $ list = explode (",", $ str); $ r = count ($ list)-1; $ laststr = ""; for ($ I = 0; $ I <4; $ I ++) {$ randnum = mt_rand (0, $ r); $ laststr. = $ list [$ randnum]; // extract the character and combine it into the string to be displayed.} $ _ SESSION ['code'] = $ laststr; // put the string into the SESSION // echo $ laststr; $ img = imagecreate (28, 28); // generate an image $ black = imagecolorallocate ($ img, 0 ); // set the color $ white = imagecolorallocate ($ img, 255,255,255); $ gray = imagecolorallocate ($ img, 200,200,200); $ red = imagecolorallocate ($ img, 0 ); $ cl = imagefill ($ img, $ red); // fill the image with color. // add the verification method to the image imagestring ($ img, 8, $ laststr, $ black); // place the verification code on the image for ($ I = 0; $ I <8; $ I ++) {$ lineColor = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255); imageline ($ img, rand (0, $ x), 0, rand (0, $ x ), $ y, $ lineColor) ;}// interference point for ($ I = 0; $ I <250; $ I ++) {imagesetpixel ($ img, rand (0, $ x), rand (0, $ y), $ fontColor) ;}$ aa = imagepng ($ img); $ bb = imagedestroy ($ img ); // echo $ aa ;}
Index. php on The View page
《script》function create_code(){ document.getElementById('code').src = '/index/create_code/'+Math.random()*10000;}《script》
You can try it.