Home > Backend Development > PHP Tutorial > A useful PHP verification code class example sharing_PHP tutorial

A useful PHP verification code class example sharing_PHP tutorial

WBOY
Release: 2016-07-13 17:18:51
Original
922 people have browsed it

Share a useful PHP verification code class, including calling examples.
Note:
If the specified font is not applicable, then use the imagestring() function. If you need to encounter the specified font, you must use the imagettftext() function. The location of the font is Windows/Fonts.

under the C drive

Refer to the method of generating verification code in PHP on the Internet, as well as the method of generating PHP image verification code and PHP Chinese verification code. Used relevant knowledge of PHP GD library.

1, the class that generates the verification code VerificationCode.class.php

Copy the code The code is as follows:

class VerificationCode{
private $charset="abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; //Random factor
private $code; //Verification code
private $codelen=4 ; //Verification code length
private $width=110; //Width
private $height=30; //Height
private $img; //Image resource handle
private $font; / /Specify font
private $fontSize=25; //Font size
private $fontColor; //Font color
public function __construct(){
$this->font="CALIBRIZ.TTF “; ; $i < $this->codelen; $i++) {
                                                      }
//Generate background
private function createBg(){
$this->img=imagecreatetruecolor($this->width,$this->height);
$color = imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255)); ;width,0,$color);
                                                                   🎜> for ($i = 0; $i < $this->codelen; $i++) {
fontColor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand( 0,156),mt_rand(0,156)); this->height/1.4,$this->fontColor,$this->font,$this->code[$i]); // www.jbxue.com
                                      // imagestring($this ->img,5,$i*$x+mt_rand(1,5),5,$this->code[$i],$this->fontColor); 🎜> //Generate lines and snowflakes
private function createDisturb(){
for ($i = 0; $i < 6; $i++) {
color $color=imageallocate($this-> img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); ;width),mt_rand(0,$this->width),mt_rand(0,$this->width),$color); 
            for ($i = 0; $i < 100; $i++) { 
                $color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); 
                imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color); 
            } 
        } 
        //输出 
        private function outPut(){ 
            header("Content-Type:image/png"); 
            imagepng($this->img); 
            imagedestroy($this->img); 
        } 
        public function showCode(){ 
            $this->createBg(); 
            $this->createCode(); 
            $this->createDisturb(); 
            $this->createFont(); 
            $this->outPut(); 
        } 
        //获取验证码 
        public function getCode(){ 
            return strtolower($this->code); 
        } 
    } 
?>

code.php

复制代码 代码如下:

    session_start(); 
    require_once 'VerificationCode.class.php'; 
    $code=new VerificationCode(); 
    $_SESSION['code']=$code->getCode(); 
    $code->showCode(); 
?> 

验证码:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/621648.htmlTechArticle分享一个好用的php验证码类,包括调用示例。 说明: 如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用...
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