Home  >  Article  >  php教程  >  一个简单的php图形验证码生成程序

一个简单的php图形验证码生成程序

WBOY
WBOYOriginal
2016-06-13 10:04:06981browse

生成验证码原理相当简单就是利用mt_rand随机生成一个数字,然后保存到session中用来用户登录时判断输入的验证码与我们生成的是否一致,然后就是把随机数字利用php gd函数生成一张图片,这样就完成了验证码的生成了。

 代码如下 复制代码

/**
 *
 * @file imgvcode.php
 * @create date 2007-09-25
 * @copyright (c) 2005 - 2007 eifr.com
 * @license http://www.hzhuti.com/nokia/n97/

 * eifr is free software
 */

session_start();

// main
$vcodes = '';
//generate Number 4
srand((double)microtime()*1000000);
for($i=0;$i     $vcodes.=rand(1,9);
}

$_SESSION['eifr_checkvcode'] = $vcodes;

if(function_exists('imagecreate')){
    //generate picture validation code
    Header("Content-type: image/PNG");

    $img = imagecreate(44,18);
    $bg = ImageColorAllocate($img, 245,245,245);
    imagefill($img,0,0,$bg); //background

   
    //generate Number 4
    for($i=0;$i         $font = ImageColorAllocate($img, rand(100,255),rand(0,100),rand(100,255));
        $vnum = substr($vcodes, $i, 1);
        imagestring($img, 5, 2+$i*10, 1, $vnum, $font);
    }

    //add interference
    for($i=0;$i     {
        $randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
        imagesetpixel($img, rand()%70 , rand()%30 , $randcolor);
    }
    ImagePNG($img);
    ImageDestroy($img);
}

?>

注:php生成验证码需要开启php gd图片库哦,要不是不能生成的。

Statement:
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