Php Chinese Character Verification Code Program

Source: Internet
Author: User

This article uses three files to briefly explain how to apply the Chinese verification code in php. Because Chinese characters may contain garbled characters, we have defined a file for special processing, for more information, see.

The Code is as follows: Copy code


<? Php
/*
* File: chinesechar. php
* Role: storage of Chinese character data
*/
$ 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 ", "Insects", "fish", "Nine", "Net", "new", "degree", "Ah", "Alas", "Ah", "Oh ", "Yi", "old", "shao", "day", "month", "Star ");
?>

<? Php
/*
* File: check. php
* Function: Verification

*/
Session_start ();
$ ErrorMSG = '';
// Verify that the user input is consistent with the verification code
If (! Is_null ($ _ POST ['check'])
{
If (strcasecmp ($ _ SESSION ['code'], $ _ POST ['code']) = 0)
$ ErrorMSG = "<p style =" font-size: 12px; color: #009900 "> Verification Successful! </P> ";
Else
$ ErrorMSG = "<p style =" font-size: 12px; color: # FF0000 "> Verification Failed! </P> ";
}
?>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<? Php
If ($ errorMSG ){
Echo $ errorMSG;
}
?>
<Form action = <? Php echo $ _ SERVER ['php _ SELF ']?> Method = post>
Enter the verification code: <input type = "text" name = "code" style = "width:
80px ">
<Br>
<Input type = "submit" name = "check" value = "submit verification code">
</Form>
</Body>
</Html>

<? Php
/*
* File: code. php
* Function: generate a verification code

* Special Note: All copyrights are copyrighted by grass without teeth! Gains can be gained only when you have paid!
*/
Include_once ("chinesechar. php ");
Session_start ();
// Set content-type
Header ("Content-type: image/png ");
// Create an image
$ Im = imagecreatetruecolor (120, 30 );

// 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
$ Font = 'simkai. ttf ';

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

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

If you want to change the above program to an English number, you only need to change the Chinese character of the array in chinesechar. php to a number or a letter to KO.

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.