PHP simple verification code: random distortion of letters + numbers

Source: Internet
Author: User
Now there are various types of verification codes. I personally think that the verification code is for users to see, so it is easy. What interference codes, skews, and complex backgrounds are all a kind of torture to users. The website verification codes with relatively large experience are relatively simple and there is no complicated background and no interference code... syntaxHighlighter. all (); now all kinds of verification codes, I personally think that the verification code is for users to see, so it is easy. What interference codes, skews, and complex backgrounds are all a kind of torture to users.
Website verification codes with relatively large experience are relatively simple, with no complex background, no interference code, and no background.
If something is not well written, you are welcome to criticize and give some advice.
[Php]
If (! Defined ('is _ initphp') exit ('Access Denied! ');
/*************************************** **************************************** **
* InitPHP 2.0 domestic PHP development framework extension class library-verification code
*-------------------------------------------------------------------------------
* CopyRight: CopyRight By initphp.com
* You can use the source code freely, but keep the author information during use. Respecting Others means respecting yourself.
*-------------------------------------------------------------------------------
* $ Author: liuxinming
* $ Dtime: 2012-10-09
**************************************** **************************************** ***/
Class seccodeInit {
Private $ width;
Private $ height;
Private $ type = 0; // 0 letters + digit verification code
Private $ time = 3000; // verification code Expiration time (s)
Private $ color = null; // font color of the verification code
Private $ im;
Private $ length = 4; // verification code length
Private $ warping; // random twist

/**
* Obtain random values
* @ Return string
*/
Private function get_random_val (){
$ I = 0;
While ($ I <$ this-> length)
{
Mt_srand (double) microtime () * 1000000 );
$ Randnum = mt_rand (50, 90 );
If (! In_array ($ randnum, array (58,59, 60,61, 62,63, 64,73, 79 )))
{
$ Authnum = $ authnum. chr ($ randnum );
$ I ++;
}
}
Session_start ();
$ Time = time ();
$ Checkcode = md5 (md5 ($ authnum. 'initphpyzmsy'. $ time ));
$ Key = $ time. ','. $ checkcode. ','. authnum;
$ _ SESSION ['initphp _ Code'] = $ key;
Return $ authnum;
}

/**
* Obtain the verification code Image
* @ Param $ width
* @ Param $ height
* @ Param $ warping random font distortion switch 0 = off, 1 = on
* @ Return string
*/
Public function getcode ($ width = 140, $ height = 40, $ warping = 0 ){
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> warping = $ warping;
If ($ this-> type <2 & function_exists ('imagecreate') & function_exists ('imagecolorset') & function_exists ('imagecopyresized ') & function_exists ('imagecolorallocate') & function_exists ('imagechar ') & function_exists ('imagecolorsforindex ')&&
Function_exists ('imageline ') & function_exists ('imagecreatefromstring') & (function_exists ('imagegif') | function_exists ('imagepng ') | function_exists ('imagejpeg '))){
$ This-> image ();
}
}
/**
* Generate an image verification code
* @ Return string
*/
Public function image (){
$ This-> im = imagecreate ($ this-> width, $ this-> height); // you can specify the image background size.
Imagecolorallocate ($ this-> im, 243,251,254); // set the background
$ This-> color = imagecolorallocate ($ this-> im, mt_rand (1,120), mt_rand (1,120), mt_rand (1,120); // random color of the font of the verification code
$ TtfPath = dirname (_ FILE _). '/font/'; // font directory
$ Dirs = opendir ($ ttfPath );
$ Seccodettf = array ();
While ($ entry = readdir ($ dirs )){
If ($ entry! = '.' & $ Entry! = '.. '& In_array (strtolower (addslashes (strtolower (substr (strrchr ($ entry ,'. '), 1, 10), array ('ttf', 'ttc '))){

$ Seccodettf [] = $ ttfPath. $ entry;
}
}
$ Ttf = $ seccodettf [array_rand ($ seccodettf)]; // random font
$ Size = $ this-> type? $ This-> width/7: $ this-> width/6; // font size
Imagettftext ($ this-> im, $ size, 0, 10, $ size * 1.2, $ this-> color, $ ttf, $ this-> get_random_val ()); // Set verification code characters
If ($ this-> warping) {// random twist
$ This-> setWarping ();
}
If (function_exists ("imagepng "))
{
Header ("Content-type: image/png ");
$ Code = imagepng ($ this-> im );
} Elseif (function_exists ("imagejpeg "))
{
Header ("Content-type: image/jpeg ");
$ Code = imagejpeg ($ this-> im );
} Elseif (function_exists ("imagegif "))
{
Header ("Content-type: image/gif ");
$ Code = imagegif ($ this-> im );
}
Imagedestroy ($ this-> im );
Return $ code;
}

/**
* Check the verification code
* @ Param $ code
* @ Return bool
*/
Public function checkCode ($ code ){
Session_start ();
$ Secode = explode (',', $ _ SESSION ['initphp _ Code']);
$ Time = time ();
// Check whether the time has expired
If ($ secode [0]> $ time | $ time-$ secode [0]> $ this-> time)
{
Return false;
}
// Whether the verification code is consistent after the two-md5 key pairs
If ($ secode [1] <> md5 (md5 ($ code. 'initphzmsy'. $ secode [0]) {
Return false;
}
// Check whether the verification code string is consistent
If ($ code | $ code <> $ secode [2])
{
Return false;
}
Return true;
}

/* Random twist */
Public function setWarping (){
$ Rgb = array ();
$ Direct = rand (0, 1 );
$ Width = imagesx ($ this-> im );
$ Height = imagesy ($ this-> im );
$ Level = $ width/20;
For ($ j = 0; $ j <$ height; $ j ++ ){
For ($ I = 0; $ I <$ width; $ I ++ ){
$ Rgb [$ I] = imagecolorat ($ this-> im, $ I, $ j );
}
For ($ I = 0; $ I <$ width; $ I ++ ){
$ R = sin ($ j/$ height * 2 * M_PI-M_PI * 0.5) * ($ direct? $ Level:-$ level );
Imagesetpixel ($ this-> im, $ I + $ r, $ j, $ rgb [$ I]);
}
}
}
}
?>
Effect:

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.