A tutorial on PHP Captcha Experience

Source: Internet
Author: User
Tags set background sublime text
<?php/** Captcha Experience *date:2011-02-19*author:fdipzone*/class captcha{//class startprivate $sname = ';p ublic function __construct ($sname = ") {//$sname captcha Session Name$this->sname = $sname = ="? ' M_captcha ': $sname;} /** Generate CAPTCHA image * @param int$length * @param array$param parameters * @return img*/public function Create ($length =4, $param =array ()) {Header ("Content-type:image/png"), $authnum = $this->random ($length);//Generate Authenticode characters. $width = Isset ($param [' width '] )? $param [' width ']: 13;//text width $height = isset ($param [' height '])? $param [' Height ']: 18;//text height $pnum= isset ($param [' pnum '])? $param [' Pnum ']: 100;//number of interfering pixels $lnum= isset ($param [' lnum '])? $param [' Lnum ']: 2;//number of interfering lines $this->captcha_session ($this->sname, $authnum);//will be written in SESSION$PW = $width * $length + $PH = $height +6; $im = Imagecreate ($PW, $ph);//imagecreate () creates a new image with a blank image size of x_size and y_size.  $black = Imagecolorallocate ($im, 238,238,238);//Set background color $values = Array (mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph)); Imagefilledpolygon ($im, $values, 6, Imagecolorallocate ($im, Mt_rand (170,255), Mt_rand (200,255), Mt_rand (210,255))); /Set up a disturbed polygon basemap/* Text */for ($i = 0; $i < strlen ($authnum); $i + +) {$font = Imagecolorallocate ($im, Mt_rand (0,50), Mt_rand (0,150 ), Mt_rand (0,200));//Set text color $x = $i/$length * $PW + rand (1, 6);//set random x Coordinate $y = rand (1, $PH/3);//set random y-coordinate imagestring ($im, Mt_r and (4,6), $x, $y, substr ($authnum, $i, 1), $font); }/* Add interfering pixel */for ($i =0; $i < $pnum; $i + +) {$dist = Imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255 )); Set the dot color Imagesetpixel ($im, Mt_rand (0, $PW), Mt_rand (0, $ph), $dist); }/* Join the */for ($i =0; $i < $lnum; $i + +) {$dist = Imagecolorallocate ($im, Mt_rand (50,255), Mt_rand (150,255), Mt_rand ( 200,255)); Setup Line Color Imageline ($im, Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), $dist);} Imagepng ($im);//Export the image to a browser or file Imagedestroy ($im) in PNG format;//Destroy an image}/** check the certificate *@param String $captcha Experience * @param int $flag after successful 0: do not clear session 1: Clear session* @return boolean*/public function Check ($captch A, $flag =1) {if (empty ($captcha)) {return false;} Else{if (Strtoupper ($captcha) = = $this->captcha_session ($this->sname)) {//Test if ($flag ==1) {$this Captcha_session ($this->sname, ');} return true;} Else{return false;}}} /* Generates a random number function * @paramint $length the number of strings that need to be randomly generated * @returnString */private function random ($length) {$hash = '; $chars = ' ABCDEFGHIJKLMNPQRSTUVWXYZ23456789 '; $max = strlen ($chars)-1;for ($i = 0; $i < $length; $i + +) {$hash. = $chars [Mt_rand (0 , $max)];} return $hash;} /** Experience the Session method * @paramString $namecaptcha session name* @paramString $value * @returnString */private function captcha _session ($name, $value =null) {if (Isset ($value)) {if ($value!== ") {$_session[$name] = $value;} Else{unset ($_session[$name]);}} Else{return isset ($_session[$name])? $_session[$name]: ';}} Class end?>

Demo

<?session_start (); require_once (' Captcha.class.php '); $obj = new Captcha ($sname); # Create Captcha like Sname to save the session name of CAPTCHA, leave it blank, and leave it as ' M_captcha ' $obj->create ($length, $param); # Create a captcha and output a picture # $ Length is CAPTCHA, can be left blank, implicitly 4/* $param = Array (' width ' = 13captcha character width ' height ' + 18captcha character height ' pnum ' = 100 Disturb dot ' Lnum ' + 2 Dry line number can be left blank */$obj->check ($captcha, $flag); # Check to see if the user input is correct, true or false# $captcha for the user to enter the certificate, required # $flag Can be left blank, 1 #1: When proven successful, self-purge captcha session#0: When proven successful, do not clear the CAPTCHA session and use Ajax to check?>

This article explains the content of PHP Captcha experience, and more about it, please follow the PHP Chinese web.

Related recommendations:

MySQL INFORMATION_SCHEMA related content

View MySQL database size, table size, and last modified time

Detailed Sublime Text 2

Related Article

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.