Three PHP Chinese verification code generation and transfer method

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array code file function image network network programming operation

Three PHP tutorial Chinese verification code generation and transfer method Generate Chinese verification code in PHP must be generated and generated verification verification code is not the same operation, because gd function only accepts utf8 format encoded text, so use php to generate Chinese verification Before the first code to be encoded conversion, operation iconph icon can be an instance of PHP.
* /

$ ch_str = "you want to generate Chinese verification code Chinese";
$ str = array ();
for ($ i = 0; $ i <strlen ($ ch_str); $ i + = 3)
{
$ str [] = $ ch_str [$ i]. $ ch_str [$ i + 1]. $ ch_str [$ i + 2];
}
// picture length and height
$ image_x = 200;
$ image_y = 100;
$ im = imagecreate ($ image_x, $ image_y);
// Here to take the picture background is white
$ bkg = imagecolorallocate ($ im, 255,255,255);
// Display the font style, this should put the file into the corresponding directory, if you do not have the file go to the window's font file to find one.
$ fnt = "simfang.ttf";
// Assign some color to the image
$ white = imagecolorallocate ($ im, 234,185,95);
// draw an elliptical arc on the picture, specify the coordinates of the next point
imagearc ($ im, 150, 8, 20, 20, 75, 170, $ white);
imagearc ($ im, 180, 7,50, 30, 75, 175, $ white);
// draw a line in the picture, specify the coordinates of the next point
imageline ($ im, 20,20,180,30, $ white);
imageline ($ im, 20,18,170,50, $ white);
imageline ($ im, 25,50,80,50, $ white);
// random number of points
$ noise_num = 3000;
$ line_num = 80;
// the color of various chaotic 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 at a coordinate point, defined above, 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 the two coordinates, 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 even
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 two
$ str = "Chinese characters";
$ image_x = 110;
$ image_y = 110;
$ im = imagecreate ($ image_x, $ image_y);
$ bkg = imagecolorallocate ($ im, 255,255,255);
$ fnt = "hb.ttf"; // Display the 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 character is in $ str1

imagettftext ($ 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 an array
/ *
gd function only accepts utf8 format encoding of the text, so before writing the first code to be converted. php comes with the iconv and mbstring library can complete this work
* /

$ randcode = array ('pet');
$ codetable = array ();
$ fp = fopen ("gb2312.txt", "r");
while ($ line = fgets ($ fp))
$ codetable [hexdec (substr ($ line, 0,6))] = substr ($ line, 7,6);
fclose ($ fp);

// gb2312 turn 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 <strlen ($ utf8); $ i + = 3)
$ ret. = chr (substr ($ utf8, $ i, 3));
}
else
{
$ ret. = substr ($ gbstr, 0,1);
$ gbstr = substr ($ gbstr, 1, strlen ($ gbstr));
}
}
return $ ret;
}

// unicode turn 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;
}

// generate additional code
function create_excode ($ length)
{
global $ randcode;
header ("content-type: image / png");
$ image_x = $ length * 30; // picture width
$ image_y = 40; // picture height
$ noise_num = 80 * $ length; / / number of impurities
$ line_num = $ length-2; / / interference line number
$ image = imagecreate ($ image_x, $ image_y);
imagecolorallocate ($ image, 0xff, 0xff, 0xff); // Set the background color
$ rectangle_color = imagecolorallocate ($ image, 0xaa, 0xaa, 0xaa); // border color
$ noise_color = imagecolorallocate ($ image, 0x00,0x00,0x00); // Miscellaneous color
$ font_color = imagecolorallocate ($ image, 0x00,0x00,0x00); // font color
$ line_color = imagecolorallocate ($ image, 0x33,0x33,0x33); // Interference line color

// Add noise
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 html syntax directly: <img src = "excode.php"> Call on it, when the server to do the verification take the session storage of the verification character and the user submitted character comparison, the same is verified

Related Article

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.