


Three PHP Chinese verification code generation and calling methods_PHP tutorial
Three PHP Chinese verification code generation and calling methods To generate a Chinese verification code in PHP, you must do a different operation than generating a verification code. Because the GD function only accepts text encoded in UTF8 format, when using PHP to generate a Chinese verification code, you must first perform encoding conversion. The iconv of operating php can be instantiated.
Three PHP tutorials Chinese verification code generation and calling methods
To generate a Chinese verification code in PHP, you must do a different operation than generating a verification code. Because the gd function only accepts text encoded in UTF8 format, when using PHP to generate a Chinese verification code, you must first perform encoding conversion. The iconv of operating php can be instantiated.
*/
$ch_str="You want to generate Chinese verification code with Chinese characters";
$str=array();
for ($i=0;$i{
$str[]=$ch_str[$i].$ch_str[$i+1].$ch_str[$i+2];
}
//The length and height of the picture
$image_x=200;
$image_y=100;
$im = imagecreate($image_x,$image_y);
//The background color of the picture here is white
$bkg = imagecolorallocate($im,255,255,255);
//Displayed font style, this requires placing the file in the corresponding directory. If you don't have a file, go find one in the window's font file.
$fnt = "simfang.ttf";
//Assign some color to the image
$white=imagecolorallocate($im,234,185,95);
//Draw an elliptical arc on the picture and specify the lower coordinate point
imagearc($im, 150, 8, 20, 20, 75, 170, $white);
imagearc($im, 180, 7,50, 30, 75, 175, $white);
//Draw a line segment on the picture and specify the lower coordinate point
imageline($im,20,20,180,30,$white);
imageline($im,20,18,170,50,$white);
imageline($im,25,50,80,50,$white);
//The number of random points
$noise_num=3000;
$line_num=80;
//Color of various confusing characters
$rectangle_color=imagecolorallocate($im,0xaa,0xaa,0xaa);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++)
{
//Draw a single pixel on a coordinate point. This point is defined above and is black.
imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
}for($i=0;$i<$line_num;$i++)
{
$line_color=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
//Draw a line between two coordinate points, the color is defined above
Imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
}
$randnum=rand(0,count($str)-4);
//Keep it an even number
if ($randnum%2)
{
$randnum+=1;
}
$str1=$str[$randnum].$str[$randnum+1];
for ($i=0;$i<2;$i++)
{
imagettftext($im, rand(28,32), rand(0,70), rand(($image_x/4)*$i+$image_x/10,($image_x/4)*$i+$image_x/8), rand($image_y/2+$image_y/10,$image_y/2+$image_y/5), $font_color, $fnt, $str[$randnum+$i]);
}
imagepng($im);
imagedestroy($im);//Generate Chinese verification code 2
$str="Chinese characters";
$image_x=110;
$image_y=110;
$im = imagecreate($image_x,$image_y);
$bkg = imagecolorallocate($im,255,255,255);
$fnt = "hb.ttf"; //Displayed font style
$white=imagecolorallocate($im,234,185,95);
imagearc($im, 150, 8, 20, 20, 75, 170, $white);
imagearc($im, 180, 7,50, 30, 75, 175, $white);
imageline($im,20,20,180,30,$white);
imageline($im,20,18,170,50,$white);
imageline($im,25,50,80,50,$white);
$noise_num=3000;
$line_num=80;
imagecolorallocate($im,0xff,0xff,0xff);
$rectangle_color=imagecolorallocate($im,0xaa,0xaa,0xaa);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
$line_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++)
imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
for($i=0;$i<$line_num;$i++)
imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
$randnum=rand(0,strlen($str)-4);
if ($randnum%2)$randnum+=1;
$str1=substr($str,$randnum,4);
$str2 = iconv("gb2312","utf-8",$str1);//Verify that the Chinese characters are in $str1imagettftext($im, rand(28,32), rand(0,70), rand(25,27), rand(70,100), $font_color, $fnt, $str2);
imagepng($im);
imagedestroy($im);//Put Chinese characters in the array
/*
The gd function only accepts text encoded in utf8 format, so encoding conversion must be performed first before writing text. Both the iconv and mbstring libraries that come with PHP can complete this work
*/$randcode=array('宠');
$codetable=array();
$fp=fopen("gb2312.txt","r");
while($line=fgets($fp))
$codetable[hexdec(substr($line,0,6))]=substr($line,7,6);
fclose($fp);//gb2312转utf8
function gb2utf8($gbstr)
{
global $codetable;
if(trim($gbstr)=="")
return $gbstr;
$ret="";
$utf8="";
while($gbstr)
{
if(ord(substr($gbstr,0,1))>127)
{
$thisw=substr($gbstr,0,2);
$gbstr=substr($gbstr,2,strlen($gbstr));
$utf8="";
@$utf8=u2utf8(hexdec($codetable[hexdec(bin2hex($thisw))-0x8080]));
if($utf8!="")
for($i=0;$i$ret.=chr(substr($utf8,$i,3));
}
else
{
$ret.=substr($gbstr,0,1);
$gbstr=substr($gbstr,1,strlen($gbstr));
}
}
return $ret;
}
//unicode转utf8
function u2utf8($c)
{
$str="";
if($c<0x80)
$str.=$c;
elseif($c<0x800)
{
$str.=(0xc0|$c>>6);
$str.=(0x80|$c&0x3f);
}
elseif($c<0x10000)
{
$str.=(0xe0|$c>>12);
$str.=(0x80|$c>>6&0x3f);
$str.=(0x80|$c&0x3f);
}
elseif($c<0x200000)
{
$str.=(0xf0|$c>>18);
$str.=(0x80|$c>>12&0x3f);
$str.=(0x80|$c>>6&0x3f);
$str.=(0x80|$c&0x3f);
}
return $str;
}//生成附加码
function create_excode($length)
{
global $randcode;
header("content-type: image/png");
$image_x=$length*30; //图片宽度
$image_y=40; //图片高度
$noise_num=80*$length; //杂点数量
$line_num=$length-2; //干扰线数量
$image=imagecreate($image_x,$image_y);
imagecolorallocate($image,0xff,0xff,0xff); //设定背景颜色
$rectangle_color=imagecolorallocate($image,0xaa,0xaa,0xaa); //边框颜色
$noise_color=imagecolorallocate($image,0x00,0x00,0x00); //杂点颜色
$font_color=imagecolorallocate($image,0x00,0x00,0x00); //字体颜色
$line_color=imagecolorallocate($image,0x33,0x33,0x33); //干扰线颜色//加入杂点
for($i=0;$i<$noise_num;$i++)
imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);$font_face="simkai.ttf"; //Font
$x=2;
$session_code='';
for($i=0;$i<$length;$i++)
{
$code=$randcode[mt_rand(0,count($randcode)-1)];
Imagettftext($image,18,mt_rand(-6,6),$x,29,$font_color,$font_face,gb2utf8($code));
$x+=30;
$session_code.=$code;
}
@session_start();
$_session['excode']=$session_code; //Put the value of the additional code in the session
//Add interference line
for($i=0;$i<$line_num;$i++)
Imageline($image,mt_rand(0,$image_x),mt_rand(0,$image_y),
mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color); //Add a border
imagepng($image);
imagedestroy($image);
}
create_excode(6);
// When using it, use html syntax directly: Just call it. When doing verification on the server side, compare the verification characters stored in the session with the characters submitted by the user. If they are the same, the verification is passed

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,

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

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

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.
