Php Chinese letter/digit verification code

Source: Internet
Author: User
Php implements verification codes in multiple ways.
Header ("Content-type: image/png ");
// Define the header and declare the image file, preferably png, without copyright concerns;
// Generate a new four-digit integer verification code
Session_start (); // enable session;
$ Authnum_session = '';
$ Str = 'abcdefghijkmnpqrstuvwxyz1234567890 ';
// Define the numbers and letters used to display on the image;
$ L = strlen ($ str); // Obtain the string length;
// Cyclically and randomly extract four letters and numbers defined above;
For ($ I = 1; $ I <= 4; $ I ++)
{
$ Num = rand (0 L-1 );
// A number is randomly selected at a time. from the first word to the maximum length of the string,
// Minus 1 is because the truncation character starts from 0, so that any 34 characters may be listed in it;
$ Authnum_session. = $ str [$ num];
// Connects the characters obtained from numbers to a total of four digits;
}
Session_register ("authnum_session ");
// It is good to use the session for verification. Register the session and name it authnum_session,
// If other pages contain the image
// You can use $ _ SESSION ["authnum_session"] to call

// Generate the verification code image,
Srand (double) microtime () * 1000000 );
$ Im = imagecreate (50, 20); // image width and height;
// Black, white, and gray colors are used;
$ Black = ImageColorAllocate ($ im, 0, 0 );
$ White = ImageColorAllocate ($ im, 255,255,255 );
$ Gray = ImageColorAllocate ($ im, 200,200,200 );
// Print the four-digit integer verification code into the image
Imagefill ($ im, 68, 30, $ gray );
// Comment out the line without interfering with the line;
$ Li = ImageColorAllocate ($ im, 220,220,220 );
For ($ I = 0; $ I <3; $ I ++)
{// Add three interference lines; or do not; depending on the situation, because it may affect user input;
Imageline ($ im, rand (), rand (), rand (20, 40), rand (), $ li );
}
// The position of the character in the image;
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: generate a verification code
*/
Session_start ();
// Set content-type
Header ("Content-type: image/png ");
// Create an image
$ Im = imagecreatetruecolor (120, 30 );
$ ChineseChar = array ("person", "out", "come", "friend", "", "filial piety", "benevolence", "righteousness ", "Li", "Lian", "Zhong", "Guo", "Zhong", "Yi", "White", "man", "fire", "Earth ", "Gold", "Wood", "Lei", "wind", "Dragon", "Tiger", "Day", "Earth ",
"Sheng", "Dizzy", "Dish", "Bird", "Tian", "3", "Bai", "Qian", "Fu", "Love ", "Emotion", "Beast", "worm", "fish", "Nine", "net", "New", "degree", "ah", "alas ", "Ah", "oh", "Yi", "old", "less", "Day ",
"Month", "star ");
// Create a color
$ Fontcolor = imagecolorallocate ($ im, 255,255,255 );
$ Bg = imagecolorallocate ($ im, 0, 0, 0 );

// Set text
For ($ I = 0; $ I <4; $ I ++) $ text. = $ ChineseChar [(array_rand ($ ChineseChar)];

$ _ SESSION ['code'] = $ text;

// Set the font [url] http://www.font.com.cn/downlist/s_12_3.html#/url] to download the _ GBK family of fonts, which are generally supported by the GD library!
$ Font = 'gbk. ttf ';

// Add text
Imagettftext ($ im, 18, 0, 11, 21, $ fontcolor, $ font, iconv ("GB2312", "UTF-8", $ text ));

// Output image
Imagepng ($ im );
Imagedestroy ($ im );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.