12306 Verification Code PHP Chinese alphanumeric Verification Codes implementation codes

Source: Internet
Author: User
English with numbers
Header ("Content-type:image/png");
Define header, declare picture file, preferably PNG, no copyright disturbance;
Generate a new four-bit integer verification code
Session_Start ();//open session;
$authnum _session = ";
$str = ' abcdefghijkmnpqrstuvwxyz1234567890 ';
Define the numbers and letters used to display on the image;
$l = strlen ($STR); Get the length of the string;
The loop randomly extracts four-bit letters and numbers defined earlier;
for ($i =1; $i <=4; $i + +)
{
$num =rand (0, $l-1);
Randomly extracting one digit at a time, from the first word to the maximum length of the string,
Minus 1 is because the Intercept character starts from 0, so that 34 characters may be in the line;
$authnum _session.= $str [$num];
The number of words Fulianqi will be four bits;
}
Session_register ("Authnum_session");
Using the session to do the verification is also good; register the session, the name is Authnum_session,
Other pages are included as long as they contain this image
That can be called by $_session["Authnum_session").
Generate a Captcha picture,
Srand (Double) microtime () *1000000);
$im = Imagecreate (50,20);//Picture width and height;
The main use of black and white gray three colors;
$black = Imagecolorallocate ($im, 0,0,0);
$white = Imagecolorallocate ($im, 255,255,255);
$gray = Imagecolorallocate ($im, 200,200,200);
Drawing a four-bit integer verification code into a picture
Imagefill ($im, 68,30, $gray);
If you do not have to interfere with the line, the comment is OK;
$li = Imagecolorallocate ($im, 220,220,220);
for ($i =0; $i <3; $i + +)
{//Add 3 lines of interference, or do not; Depending on the circumstances, as 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 interfering 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 a picture
$im = Imagecreatetruecolor (120, 30);
$ChineseChar = Array ("Person", "out", "Come", "friend", "Learn", "filial piety", "benevolence", "righteousness", "propriety", "low", "loyalty", "Country", "middle", "easy", "white", "person", "Fire", "Earth", "gold", "Wood", "Thunder", "Wind", "Dragon" , "Tiger", "Day", "ground",
"Sheng", "halo", "dish", "bird", "Tian", "three", "Hundred", "money", "blessing", "Love", "affection", "Beast", "Worm", "fish", "nine", "net", "new", "degree", "ay", "Alas", "ah", "Oh", "Miriam", "old", "less", "Day",
"Month", "star");
Create 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 font [Url]http://www.font.com.cn/downlist/s_12_3.html[/url] have _GBK series font download, General GD Library are supported!
$font = ' Gbk.ttf ';
Add text
Imagettftext ($im, 0, one, one, $fontcolor, $font, Iconv ("GB2312", "UTF-8", $text));
Output picture
Imagepng ($im);
Imagedestroy ($im);
?>

The above describes the 12306 verification code PHP Chinese alphanumeric Verification code implementation codes, including the content of the 12306 verification Code, I hope to be interested in PHP tutorial friends helpful.

  • 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.