Php image verification code generation-php Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.