Home > Backend Development > PHP Tutorial > PHP simple verification code class: letters + numbers, random font distortion_PHP tutorial

PHP simple verification code class: letters + numbers, random font distortion_PHP tutorial

WBOY
Release: 2016-07-13 17:54:27
Original
1125 people have browsed it

There are all kinds of verification codes now. Personally, I think the verification code is for users to see, so it’s better to keep it simple. Interference codes, tilts, and complex backgrounds are all a form of torture for users.
​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​: The verification codes of some larger websites with better experience are relatively simple, without complicated backgrounds, no interference codes, and no backgrounds.
If there is anything that is not well written, you are welcome to criticize and give pointers.
[php]
if (!defined('IS_INITPHP')) exit('Access Denied!');
/***************************************************** *******************************
* InitPHP 2.0 domestic PHP development framework extended class library-verification code
*------------------------------------------------ ----------------------------------
* Copyright: CopyRight By initphp.com
* You can use this source code freely, but please keep the author information during use. Respecting the fruits of others’ labor means respecting yourself
*------------------------------------------------ ----------------------------------
* $Author:liuxinming
* $Dtime:2012-10-09
*************************************************** **********************************/
class seccodeInit{
private $width;
private $height;
Private $type=0;// 0 letter + number verification code
Private $time=3000;//Verification code expiration time (s)
Private $color=null;//Verification code font color
private $im;
Private $length=4;//Verification code length
Private $warping;//Random twisting
       
/**
* Get random value
* @return string
​​*/
Private function get_random_val() {
         $i=0;
​​​​while($i<$this->length)
                                                                                  mt_srand((double)microtime()*1000000);
               $randnum=mt_rand(50,90);
If(!in_array($randnum,array(58,59,60,61,62,63,64,73,79)))
                                                                                        $authnum=$authnum.chr($randnum);
$i++;
                                                                                                                                               } 
session_start();
         $time=time();
         $checkcode=md5(md5($authnum.'initphpYzmsy'.$time));
          $key=$time.','.$checkcode.','.authnum;
          $_SESSION['initphp_code'] = $key;
         return $authnum;
}  
       
/**
* Get verification code picture
* @param $width width
* @param $height high
* @param $warping font random distortion switch 0=off, 1=on
* @return string
​​*/
Public function getcode($width=140,$height=40,$warping=0){
          $this->width=$width;
          $this->height=$height;
          $this->warping=$warping;
if($this->type<2&& function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex' ) &&
function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))){
$this->image();
         } 
}  
/**
* Generate image verification code
* @return string
​​*/
Public function image(){
           $this->im=imagecreate($this->width, $this->height);//Set the image background size
Imagecolorallocate($this->im, 243, 251, 254);//Set the background
           $this->color=imagecolorallocate($this->im, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120));// Random color of verification code font
            $ttfPath = dirname(__FILE__) . '/font/';//Font directory
          $dirs = opendir($ttfPath);
          $seccodettf = array();
​​​​while($entry = readdir($dirs)) {
if($entry != '.' && $entry != '..' && in_array(strtolower(addslashes(strtolower(substr(strrchr($entry, '.'), 1, 10)))), array(' ttf', 'ttc'))) {
                                                                              $seccodettf[] = $ttfPath.$entry;
                                                                                                                                               } 
          $ttf = $seccodettf[array_rand($seccodettf)];//Random font
$size = $this->type ? $this->width / 7 : $this->width / 6;//Font size
Imagettftext($this->im,$size, 0, 10, $size*1.2, $this->color, $ttf, $this->get_random_val());//Set verification code characters
If($this->warping){//Random warping
$this->setWarping();
         } 
If(function_exists("imagepng"))
                                                                  header ("Content-type: image/png");
$code=imagepng($this->im);
         }elseif (function_exists("imagejpeg"))
                                                                  header ("Content-type: image/jpeg");
$code=imagejpeg($this->im);
        }elseif (function_exists("imagegif"))  
                                                                  header("Content-type: image/gif");
$code=imagegif($this->im);
         } 
imagedestroy($this->im);
         return $code;
}  
       
/**
* Check verification code
* @param $code
* @return bool
​​*/
Public function checkCode($code) {
session_start();
          $secode=explode(',', $_SESSION['initphp_code']);
         $time=time();
//Check whether the time has expired
If($secode[0]>$time||$time-$secode[0]>$this->time)
                                                                                 return false;
         } 
//Verification code key is consistent after double md5
If($secode[1]<>md5(md5($code.'initphpYzmsy'.$secode[0]))){
             return false;
         } 
//Check whether the verification code strings are consistent
If($code||$code<>$secode[2])
                                                                                 return false;
         } 
        return true;
}  
       
/*Random distortion*/
Public function setWarping(){
          $rgb=array();
         $direct=rand(0,1);
$width = imagesx($this->im);
$height = imagesy($this->im);
         $level=$width /20;
for($j = 0;$j < $height;$j++) {
for($i = 0;$i < $width;$i++) {
                    $rgb[$i] = imagecolorat($this->im, $i, $j);
                                                                                                                    for($i = 0;$i < $width;$i++) {
                  $r = sin($j / $height * 2 * M_PI - M_PI * 0.5) * ($direct ? $level : -$level);
imagesetpixel($this->im, $i + $r, $j, $rgb[$i]);
                                                                                                                             } 
}  
}
?>
Effect:




PHP simple verification code class: letters + numbers, random font distortion_PHP tutorialhttp://www.bkjia.com/PHPjc/477945.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/477945.htmlTechArticleThere are various verification codes now. Personally, I think the verification code is for users to see, so it’s better to keep it simple. Interference codes, tilts, and complex backgrounds are all a form of torture for users. Take a look...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template