Home >Backend Development >PHP Tutorial >PHP Chinese alphanumeric verification code implementation code_PHP tutorial
Same as numbers in English
Header("Content-type:image/png");
//Define the header and declare the image file, preferably png, without copyright interference;
//Generate a new four-digit integer verification code
session_start();//Open session;
$authnum_session = '';
$str = 'abcdefghijkmnpqrstuvwxyz1234567890';
//Definition used Numbers and letters displayed on the picture;
$l = strlen($str); //Get the length of the string;
//Loop to randomly select four letters and numbers defined previously;
for ($i=1;$i<=4;$i++)
{
$num=rand(0,$l-1);
//Randomly select one number each time; from the One word to the maximum length of the string,
//Minus 1 because the intercepted characters start from 0; so any 34 characters may be ranked among them;
$authnum_session.= $str[$num ];
//The characters obtained by numbers are connected to a total of four digits;
}
session_register("authnum_session");
//It is also good to use session for verification; register session , the name is authnum_session,
//As long as other pages contain this image
//you can call it through $_SESSION["authnum_session"]
//generate the verification code image,
srand((double)microtime()*1000000);
$im = imagecreate(50,20);//Picture width and height;
//Mainly using three colors: black, white and gray;
$ black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
//Convert four digits Draw the integer verification code into the image
imagefill($im,68,30,$gray);
//If no interference lines are needed, just comment;
$li = ImageColorAllocate($im, 220,220,220);
for($i=0;$i<3;$i++)
{//Add 3 interference lines; or not; it depends on the situation, because it may affect user input;
imageline( $im,rand(0,30),rand(0,21),rand(20,40),rand(0,21),$li);
}
//The position of the character in the picture;
imagestring($im, 5, 8, 2, $authnum_session, $white);
for($i=0;$i<90;$i++)
{//Add interference pixels
imagesetpixel($im, rand()%70 , rand()%30 , $gray);
}
ImagePNG($im);
ImageDestroy($im);
?> ;
Chinese
/*
* File: code.php
* Function: Verification code generation
*/
session_start ();
// Set content-type
header("Content-type: image/png");
// Create image
$im = imagecreatetruecolor(120, 30);
$ChineseChar = array("人","出","来","Friend","Learn","Filial Piety","Benevolence","Yi","Li","Integrity","Loyalty", "Guo", "Zhong", "Yi", "White", "Zhe", "Fire", "Earth", "Gold", "Wood", "Thunder", "Wind", "Dragon", "Tiger" ","天","地",
"生","半","菜","鸟","田","三","百","钱","福 "," "Love", "love", "beast", "worm", "fish", "nine", "net", "new", "degree", "hey", "alas", "ah", "oh" ,"Yi","老","小","日",
"月","星"); , 255);
$bg = imagecolorallocate($im, 0, 0, 0);
// Set text
for($i=0;$i<4;$i++) $text .= $ChineseChar[(array_rand($ChineseChar))];
$_SESSION['code'] = $text;
// Set font [url]http:// www.font.com.cn/downlist/s_12_3.html[/url] There are _GBK series fonts to download, which are generally supported by GD libraries!
$font = 'gbk.ttf';
// Add text
imagettftext($im, 18, 0, 11, 21, $fontcolor, $font, iconv("GB2312","UTF-8",$text));
/ / Output image
imagepng($im);
imagedestroy($im);
?>
http://www.bkjia.com/PHPjc/319031.html