PHP CAPTCHA Authentication Code class

Source: Internet
Author: User
Tags hash rand strlen
<?php/** Captcha Certificate * date:2011-02-19 * Author:fdipzone/class captcha{//class start  
      
    Private $sname = '; Public function __construct ($sname = ") {//$sname CAPTCHA session name $this->sname = $sname = = '?  
    ' M_captcha ': $sname; 
    /** generate a CAPTCHA picture * @param int $length authentication length * @param Array $param parameters * @return IMG  
        * * Public Function Create ($length =4, $param =array ()) {Header ("content-type:image/png");   $authnum = $this->random ($length);  
          
        Generates a CAPTCHA character. $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 jamming pixels $lnum = isset ($param [' lnum '])?        $param [' Lnum ']: 2;                Number of interference lines $this->captcha_session ($this->sname, $authnum); WillRandom numbers are written into the session $PW = $width * $length +10;  
                      
        $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); Sets the background color $values = Array (mt_rand (0, $PW), Mt_rand (0, $ph), Mt_ran D (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 to disturb the polygon/* text/for ($i = 0; $i < strlen ($authnum); $i + +) {$font = Imag Ecolorallocate ($im, Mt_rand (0,50), Mt_rand (0,150), Mt_rand (0,200));/Set text color $x = $i/$length * $PW + rand (1, 6);                   Set the random x-coordinate $y = rand (1, $PH/3);   
        Set random y-coordinate imagestring ($im, Mt_rand (4,6), $x, $y, substr ($authnum, $i, 1), $font); }/* Add the jamming pixel */for ($i =0 $i < $pnum $i + +) {$dist = Imagecolorallocate ($im, Mt_ran D (0,255), Mt_rand (0,255), Mt_rand (0,255));   
        Set the miscellaneous point color Imagesetpixel ($im, Mt_rand (0, $PW), Mt_rand (0, $ph), $dist); }/* Add the =0/for ($i; $i < $lnum; $i + +) {$dist = Imagecolorallocate ($im, Mt_ran D (50,255), Mt_rand (150,255), Mt_rand (200,255));  
        Set Line Color Imageline ($im, Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), $dist);      } imagepng ($im);  Output the image to the browser or file Imagedestroy ($im) in PNG format; Destroy an image}/** inspection certificate * @param String $captcha Verify * See more highlights in this column: HTTP://WWW.BIANCENG.CNHT tp://www.bianceng.cn/webkf/php/* @param int $flag VerificationAfter success 0: do not clear session 1: Clear Session * @return Boolean/Public function check ($captcha, $flag =1) {if (EM)  
        Pty ($captcha)) {return false; }else{if (strtoupper ($captcha) = = $this->captcha_session ($this->sname)) {//Verify the test code if (  
                $flag ==1) {$this->captcha_session ($this->sname, ");  
            return true;  
            }else{return false; * * Generate random number function * @param int $length need to randomly generate string numbers * @return Str  
        ing */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; /** Verification Session Processing method * @param String $name CAPTCHA Session name * @param string $value * @return string/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 # $sname to save Captcha's session name, leave it blank and leave it blank for ' M_captcha ' $obj-&    Gt;create ($length, $param);  
                                    # Create CAPTCHA and output a picture # $length for Captcha, you can leave it blank and think 4 
                                            /* $param = Array (' width ' => captcha character width         ' Height ' => captcha character height ' pnum ' => 100 
                                            To disturb the number of ' Lnum ' => 2. ) can be left blank/* $obj ; Check ($captcha, $flag);  
    # Check to see if the user input test is correct, true or False # $captcha for the user input certificate, required                                # $flag can be left blank for 1 # 1: Automatically clear Captcha Sessio after successful verification N # 0: When the experience is successful, do not clear captcha session, used for Ajax check?>

Author: csdn blog proud Snow star Maple

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.