Analysis of php imitation QQ verification code examples

Source: Internet
Author: User
This article describes an example of a php-based QQ-like verification code, a verification code for QQ registration or logon. you have all seen it. the code provided in this article, you can also implement such a verification code. For more information, see.

The code is as follows:


/**
* QQ verification code imitation
*/
// Session save path
$ SessSavePath = dirname (_ FILE _). "/../data/sessions /";
If (is_writeable ($ sessSavePath) & is_readable ($ sessSavePath) {session_save_path ($ sessSavePath );}
Session_start ();
// Obtain random characters
$ Rndstring = '';
For ($ I = 0; $ I <4; $ I ++) $ rndstring. = chr (mt_rand (65,90 ));
$ Img_height = 45; // define the length and width of the image first.
$ Img_width = 10;
// If GD is supported, draw
If (function_exists ("imagecreate "))
{
// In some cases in Firefox, there will be multiple requests, and refreshing the page within 5 seconds will not change the session
$ Ntime = time ();
If (empty ($ _ SESSION ['DD _ ckstr_last ']) | empty ($ _ SESSION ['DD _ ckstr']) | ($ ntime-$ _ SESSION ['DD _ ckstr_last ']> 5 ))
{
$ _ SESSION ['DD _ ckstr'] = strtolower ($ rndstring );
$ _ SESSION ['DD _ ckstr_last '] = $ ntime;
}
$ Rndstring = $ _ SESSION ['DD _ ckstr'];
$ Rndcodelen = strlen ($ rndstring );
// Create an image and set the background color
$ Im = imagecreate (46,20 );
ImageColorAllocate ($ im, 240,243,248 );
// Interference line
$ LineColor1 = ImageColorAllocate ($ im, mt_rand (174,218), mt_rand (190,225), mt_rand (217,237 ));
For ($ j = 1; $ j <= 2; $ j = $ j + 3)
{
Imageline ($ im, 0, $ j + mt_rand (1, 15), 48, $ j + mt_rand (1, 15), $ lineColor1 );
}
// Output text
$ FontColor = ImageColorAllocate ($ im, mt_rand (0,150), mt_rand (0,150), mt_rand (0,150 ));
For ($ I = 0; $ I <$ rndcodelen; $ I ++)
{
$ Bc = mt_rand (0, 1 );
$ Rndstring [$ I] = strtoupper ($ rndstring [$ I]);
Imagestring ($ im, mt_rand (3, 5), $ I * $ img_height/4 + mt_rand (1, 5), mt_rand (1, $ img_width/2), $ rndstring [$ I], $ fontColor );
}
Header ("Pragma: no-cache" r "n ");
Header ("Cache-Control: no-cache" r "n ");
Header ("Expires: 0" r "n ");
// Output a specific image format with a priority of gif-> jpg-> png
If (function_exists ("imagepng "))
{
Header ("content-type: image/png" r "n ");
Imagepng ($ im );
}
Else
{
Header ("content-type: image/jpeg" r "n ");
Imagejpeg ($ im );
}
ImageDestroy ($ im );
Exit ();
}
Else
{
// GD is not supported. only the English letter ABCD is output.
$ _ SESSION ['DD _ ckstr'] = "abcd ";
$ _ SESSION ['DD _ ckstr_last '] = '';
Header ("content-type: image/png" r "n ");
Header ("Pragma: no-cache" r "n ");
Header ("Cache-Control: no-cache" r "n ");
Header ("Expires: 0" r "n ");
$ Fp = fopen ("data/vdcode.jpg", "r ");
Echo fread ($ fp, filesize ("data/vdcode.jpg "));
Fclose ($ fp );
Exit ();
}
?>

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.