


PHP image verification code generation program code_PHP tutorial
There are two verification code generation programs here. The first one is better, and the second one is also good. At the same time, the second generation program uses a complete example to illustrate the verification code calling and generation.
There are two verification code generation programs here. The first version of the verification code generation program is better, and the second one is also good. At the same time, the second generation program uses a complete example to illustrate the calling and generation of the verification code. */
session_start();
class authnum {
//Image object, width, height, verification code length
private $im;
private $im_width;
private $im_height;
private $len;
//Random string, y-axis coordinate value, random color
private $randnum;
private $y;
private $randcolor;
//Background color red, green and blue, default is light gray
public $red=238;
public $green=238;
public $blue=238;
/**
* Optional settings: verification code type, interference point, interference line, y-axis random
* Set to false to disable
**/
//The default is a mixture of uppercase and lowercase numbers, 1 2 3 respectively represents lowercase, uppercase, and numbers
public $ext_num_type='';
public $ext_pixel = false; //interference point
public $ext_line = false; //interference line
public $ext_rand_y= true; //y-axis random
function __construct ($len=4,$im_width='',$im_height=25) {
// Verification code length, image width, and height are required data when instantiating the class
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
//Set the image background color, the default is light gray background
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// Get a random code of any number
function get_randnum () {
$an1 = 'abcdefghijklmnopqrstuvwxyz';
$an2 = 'abcdefghijklmnopqrstuvwxyz';
$an3 = '0123456789';
if ($this->ext_num_type == '') $str = $an1.$an2.$an3;
if ($this->ext_num_type == 1) $str = $an1;
if ($this->ext_num_type == 2) $str = $an2;
if ($this->ext_num_type == 3) $str = $an3;
for ($i = 0; $i < $this->len; $i++) {
$start = rand(1,strlen($str) - 1);
$randnum .= substr($str,$start,1);
}
$this->randnum = $randnum;
$_session[an] = $this->randnum;
}
// Get the y-axis of the verification code image
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
else $this->y = $this->im_height / 4 ;
}
// Get random color
function get_randcolor () {
$this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
}
//Add interference points
function set_ext_pixel () {
if ($this->ext_pixel) {
for($i = 0; $i < 100; $i++){
$this->get_randcolor();
imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
}
}
}
//Add interference line
function set_ext_line () {
if ($this->ext_line) {
for($j = 0; $j < 2; $j++){
$rand_x = rand(2, $this->im_width);
$rand_y = rand(2, $this->im_height);
$rand_x2 = rand(2, $this->im_width);
$rand_y2 = rand(2, $this->im_height);
$this->get_randcolor();
Imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
}
}
}
/**Create verification code image:
* Create canvas (__construct function)
* Set canvas background ($this->set_bgcolor();)
* Get a random string ($this->get_randnum ();)
* Write text to the image (imagestring function)
* Add interference points/lines ($this->set_ext_line(); $this->set_ext_pixel();)
* Output image
**/
function create () {
$this->set_bgcolor();
$this->get_randnum ();
for($i = 0; $i < $this->len; $i++){
$font = rand(4,6);
$x = $i/$this->len * $this->im_width + rand(1, $this->len);
$this->get_y();
$this->get_randcolor();
imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
}
$this->set_ext_line();
$this->set_ext_pixel();
header("content-type:image/png");
imagepng($this->im);
imagedestroy($this->im); //释放图像资源
}}//end class
/**使用验证码类的方法:
* $an = new authnum(验证码长度,图片宽度,图片高度);
* 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片
* 表单页面检测验证码的方法,对比 $_session[an] 是否等于 $_post[验证码文本框id]
* 可选配置:
* 1.验证码类型:$an->ext_num_type=1; 值为1是小写类型,2是大写类型,3是数字类型
* 2.干扰点:$an->ext_pixel = false; 值为false表示不添加干扰点
* 3.干扰线:$an->ext_line = false; 值为false表示不添加干扰线
* 4.y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片y轴随机
* 5.图片背景:改变 $red $green $blue 三个成员变量的值即可
**/
$an = new authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //干扰点
$an->ext_line = false; //干扰线
$an->ext_rand_y= true; //y轴随机
$an->green = 238;
$an->create();
?>
好下面来看一款验证码调用实例
例子demo:
以下为引用的内容:
hi.baidu.com/ji_haiyang
Example demo1:
session_start();
$test = $_post['test'];
$test = trim($test);
$submit = $_post['submit'];
if($test==$_session['vcode']){
echo 'true, the verification code entered is correct';
} else {
echo 'false, input verification code is wrong';
}?>
Calling the file vcode.php: The following is the quoted content:
/**
* The default verification code session is vcode. That is: $_session['vcode'];
* Be careful not to conflict the variable name with the session when assigning variable values
* Note: Case-insensitive
is not used during verification*/
include("inc_vcode_class.php");
$v = new vcode;
//$config['width'] = 50; //Verification code width
//$config['height'] = 20; //Verification code high
//$config['vcode'] = "vcode"; //session
used when checking verification code //$config['type'] = "default"; //Verification code display type default: uppercase letters, string: lowercase letters, int: numbers php programmer station
//$config['length'] = 4; //Verification code length
//$config['interfere']= 10; //Interference line strength, range is 1-30, 0 or empty means the interference line is disabled
//$v->init($config); //Configuration
$v->create();
?>Verification code class inc_vcode_class.php: www~phperz~com The following is the quoted content:
/**
* Verification code class
* Note: Requires gd library support
*/
session_start();
class vcode{
private $config;
private $im;
private $str;function __construct(){
$this->config['width'] = 50;
$this->config['height'] = 20;
$this->config['vcode'] = "vcode";
$this->config['type'] = "default";
$this->config['length'] = 4;
$this->config['interfere'] = 10;$this->str['default'] = "abcdefghijklmnopqrstuvwxyz";
$this->str['string'] = "abcdefghijklmnopqrstuvwxyz";
$this->str['int'] = "0123456789";
}//Configuration
public function init($config=array()){
if (!empty($config) && is_array($config)){
foreach($config as $key=>$value){
$this->config[$key] = $value;
}
}
}//Output verification code
public function create(){
if (!function_exists("imagecreate")){
return false;
}
$this->create_do();
}//Create
private function create_do(){
$this->im = imagecreate($this->config['width'],$this->config['height']); PHP Programmer's Home
//Set the background to white
imagecolorallocate($this->im, 255, 255, 255);//Add a border to this background
$bordercolor= imagecolorallocate($this->im,37,37,37);
imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor);//Generate verification code
$this->create_str();
$vcode = $_session[$this->config['vcode']];//Enter text
$fontcolor = imagecolorallocate($this->im,46,46,46);
for($i=0;$i<$this->config['length'];$i++){
imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor);
}//Join the interference line
$interfere = $this->config['interfere'];
$interfere = $interfere>30?"30":$interfere; php programmer station
if (!empty($interfere) && $interfere>1){
for($i=1;$i<$interfere;$i++){
$linecolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
$x = rand(1,$this->config['width']);
$y = rand(1,$this->config['height']);
$x2 = rand($x-10,$x+10);
$y2 = rand($y-10,$y+10);
imageline($this->im,$x,$y,$x2,$y2,$linecolor);
}
}header("pragma:no-cachern");
header("cache-control:no-cachern");
header("expires:0rn");
header("content-type:image/jpegrn");
imagejpeg($this->im);
imagedestroy($this->im);
exit;
}//Get verification code
private function create_str(){
if ($this->config['type']=="int"){
for($i=1;$i<=$this->config['length'];$i++){
$vcode .= rand(0,9);
}
$_session[$this->config['vcode']] = $vcode;
return true;
}
$len = strlen($this->str[$this->config['type']]);
if (!$len){
$this->config['type'] = "default";
$this->create_str();
}
for($i=1;$i<=$this->config['length'];$i++){
$offset = rand(0,$len-1);
$vcode .= substr($this->str[$this->config['type']],$offset,1);
}
$_session[$this->config['vcode']] = $vcode;
return true;
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
