Simple and secure PHP verification code with call method, PHP code attached to call _php tutorial

Source: Internet
Author: User
Tags asin image png learn php learn php programming set background sin

Simple and secure PHP verification code with call method, PHP verification code attached to the call


Example of a verification code


Second, PHP Verification code class, secoder.class.php

<?php/** * Security Verification Code * * Security Code to: Verify code Text Twist, rotate, use different fonts, add noise code * * @author Running water Mengchun
 
  
  * @link Http://labs.yulans.cn/YL_Security_Secoder * @link Http://wiki.yulans.cn/docs/yl/security/secoder */class Yl_    The subscript of the session of the Security_secoder {/** * Verification code * * @var String *///public static $seKey = ' Sid.sek ey.ylans.cn ';   public static $seKey = ' Sid ';   public static $expire = 3000; Verification code Expiration Time (s)/** * The characters used in the verification code, 01IO is easy to confuse, it is recommended not to * * @var String */public static $codeSet = ' 346789ABCDEFGHJKL   Mnpqrtuvwxy ';   public static $fontSize = 25;  Captcha font size (px) public static $useCurve = true;  Whether to draw the confusion curve public static $useNoise = true;    Whether to add a miscellaneous public static $imageH = 0;    Captcha picture wide public static $imageL = 0;    Captcha picture Long public static $length = 4; Verify the number of code bits public static $BG = Array (243, 251, 254);   Background protected static $_image = NULL;   Authenticode picture instance protected static $_color = NULL; CAPTCHA Font Color/** * Output Verification code and save the value of the verification code in the session * verification code saved to session in the format: $_session[self:: $seKey] = array (' Code ' = ' Verification code Value ', ' time ' = ' Verification Code created '); */public static function entry () {//Picture width (px) Self:: $imageL | | self:: $imageL = self:: $length * Self:: $fontSize      * 1.5 + Self:: $fontSize *1.5; Picture height (px) Self:: $imageH | |     Self:: $imageH = self:: $fontSize * 2;      Create a self:: $imageL x Self:: $imageH image self::$_image = Imagecreate (self:: $imageL, Self:: $imageH);      Set Background imagecolorallocate (Self::$_image, Self:: $BG [0], Self:: $BG [1], Self:: $BG [2]);     CAPTCHA font random Color Self::$_color = imagecolorallocate (Self::$_image, Mt_rand (1,120), Mt_rand (1,120), Mt_rand (1,120)); The captcha uses a random font//$ttf = DirName (__file__). '/ttfs/'. Mt_rand (1, 20). '. TTF '; 4 $ttf = dirname (__file__).           '/ttfs/4.ttf ';     if (self:: $useNoise) {//Plot dot self::_writenoise ();     } if (self:: $useCurve) {//Draw interference line self::_writecurve (); }//Draw verification Code $code = Array (); Verification code $codeNX = 0; The left margin of the nth character of the captcha for ($i = 0; $i
  
   
 0) {Imagesetpixel (self::$_image, $px + $i, $py + $i, self::$_color);//Here the image is better than Imagettftext and imagestring performance.         Multi-$i-;         }}} $A = Mt_rand (1, Self:: $imageH/2);  Amplitude $f = Mt_rand (-self:: $imageH/4, Self:: $imageH/4); X-axis direction Offset $T = Mt_rand (self:: $imageH *1.5, Self:: $imageL);        Cycle $w = (M_PI)/$T;     $b = $py-$A * sin ($w * $px + $f)-Self:: $imageH/2;     $PX 1 = $px 2;     $px 2 = self:: $imageL;  for ($px = $px 1; $px <= $px 2, $px = $px + 0.9) {if ($w!=0) {$py = $A * Sin ($w * $px + $f) + $b + self:: $imageH/2;         y = Asin (ωx+φ) + b $i = (int) (self:: $fontSize-8)/4); while ($i > 0) {imagesetpixel (self::$_image, $px + $i, $py + $i, self::$_color);//here (while) the cycle of the image point than the IMA         Gettftext and imagestring use a font size to draw (without this while loop) a much better performance $i-; }}}}/** * Draw the dots * Write a different color letter or number on the picture */protected static function _writenoise () {for($i = 0; $i < $i + +)                 {//DOT color $noiseColor = imagecolorallocate (Self::$_image, Mt_rand (150,225),       Mt_rand (150,225), Mt_rand (150,225)); for ($j = 0; $j < 5; $j + +) {//Plot point imagestring (Self::$_image, 5, Mt_ran            D ( -10, Self:: $imageL), Mt_rand ( -10, Self:: $imageH), Self:: $codeSet [Mt_rand (0, 27)],//Miscellaneous text is a random letter or number       $noiseColor); }}}/** * Verify that the verification code is correct * * @param string $code User Verification code * @param BOOL User Verification code is correct */public static F     Unction Check ($code) {isset ($_session) | | | session_start (); The verification code cannot be empty if (empty ($code) | | empty ($_session[self:: $seKey])) {//echo $_session[self:: $seKey [' Code ']. '       1 ';                return false; }//Session expires if (time ()-$_session[self:: $seKey [' time '] > Self:: $expire) {unset ($_session[self:: $seK       EY]); Echo $_session[self:: $seKey [' Code ']. '       2 ';       return false;     return 0; }//if ($code = = $_session[self:: $seKey [' Code ']) {if (Strtoupper ($code) = = $_session[self:: $seKey] [' Code ']) {//No region Comparison of//echo $_session[self:: $seKey [' Code ']. '       3 ';         return true; }//echo $_session[self:: $seKey [' Code ']. '     4 ';            return false; }}//useage/* Yl_security_secoder:: $useNoise = false; To be more secure, change to True yl_security_secoder:: $useCurve = true; Yl_security_secoder::entry (); */*//Verify the Verification code if (!  Yl_security_secoder::check (@$_post[' Secode ')) {print ' Error secode ';} */
  
 

Third, call method

1. Display Verification code page code.php

<?php    session_start ();   Require ' secoder.class.php '; First, the class is included and the actual path is modified according to the actual situation.    $vcode = new Yl_security_secoder ();   Instantiate an object    $vcode->entry ();  ? >  

2. Check that the verification code is correct

<?php    session_start ();   Require ' secoder.class.php '; First, the class is included and the actual path is modified according to the actual situation.    $vcode = new Yl_security_secoder ();   Instantiate an object    //$vcode->entry ();    $code = $_get[' code ');    echo $vcode->check ($code);       $_session[' Code ' = $VC->getcode ();//verification code saved to SESSION?>  

3. Verification Code Input Box call page

Click the picture to retrieve the verification code again

The above is the whole content of this article, I hope that you learn PHP programming help.

http://www.bkjia.com/PHPjc/1133027.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133027.html techarticle simple and secure PHP verification code with call method, PHP verification code with call one, verification code example two, PHP verification code class, secoder.class.php PHP/** * Security Code * * Security verification ...

  • 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.