Click php to refresh the verification code

Source: Internet
Author: User
This article mainly introduces how php can refresh the verification code by clicking it. The code is very detailed and is worth learning. If you are interested, please refer to it.

This article mainly introduces how php can refresh the verification code by clicking it. The code is very detailed and is worth learning. If you are interested, please refer to it.

Verification code file CreateImg. class. php

<? Phpclass ValidationCode {private $ width, $ height, $ codenum; public $ checkcode; // generated Verification Code private $ checkimage; // Verification Code image private $ disturbColor = ''; // interfere with the pixel function _ construct ($ width = '80', $ height = '20', $ codenum = '4') {$ this-> width = $ width; $ this-> height = $ height; $ this-> codenum = $ codenum;} function outImg () {// output header $ this-> outFileHeader (); // generate a verification code $ this-> createCode (); // generate an image $ this-> createImage (); // set the interference pixel $ this-> setDi SturbColor (); // write the verification code $ this-> writeCheckCodeToImage (); imagepng ($ this-> checkimage); imagedestroy ($ this-> checkimage );} private function outFileHeader () {header ("Content-type: image/png");} private function createCode () {$ this-> checkcode = strtoupper (substr (md5 (rand (), 0, $ this-> codenum);} private function createImage () {$ this-> checkimage = @ imagecreate ($ this-> width, $ this-> height); $ back = imagecolora Llocate ($ this-> checkimage, 255,255,255); $ border = imagecolorallocate ($ this-> checkimage, 0, 0); imagefilledrectangle ($ this-> checkimage, 0, 0, $ this-> width-1, $ this-> height-1, $ back); // white background imagerectangle ($ this-> checkimage, $ this-> width-1, $ this-> height-1, $ border); // black border} private function setDisturbColor () {for ($ I = 0; $ I <= 200; $ I ++) {$ this-> disturbColor = imagecolorallocate ($ this-> checkimage, rand (0,255), rand (0,255), rand (0,255); imagesetpixel ($ this-> checkimage, rand (2,128), rand ), $ this-> disturbColor) ;}} private function writeCheckCodeToImage () {for ($ I = 0; $ I <=$ this-> codenum; $ I ++) {$ bg_color = imagecolorallocate ($ this-> checkimage, rand (0,255), rand (0,128), rand (0,255 )); $ x = floor ($ this-> width/$ this-> codenum) * $ I; $ y = rand (0, $ this-> height-15 ); imagechar ($ this-> checkimage, rand (5, 8), $ x, $ Y, $ this-> checkcode [$ I], $ bg_color) ;}} function _ destruct () {unset ($ this-> width, $ this-> height, $ this-> codenum) ;}}?>

Contains the file imgcode. php

<? Php session_start (); require_once ('createimg. class. php '); $ image = new ValidationCode ('80', '20', '4'); // image length, width, and number of characters $ image-> outImg (); $ _ SESSION ['validationcode'] = $ image-> checkcode; // store the verification code to $ _ SESSION?>

Front-End file demo. php

? Php session_start (); $ test =$ _ POST ['test']; $ test = strtoupper (trim ($ test )); $ submit = $ _ POST ['submit ']; if (isset ($ submit) {if ($ test = $ _ SESSION ['validationcode']) {echo 'true';} else {echo 'false'; }}?> Image

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.