A php code for generating verification Codes

Source: Internet
Author: User

Generate verification code a php code to generate the verification code and customize the session directory. This is not php. the default session file path in ini is saved, as shown in session_save_path below. You can redefine the directory.

Generate Verification Code
A php tutorial generates the verification code and defines the session directory. This is not the default session file storage path in php. ini, as shown below

Session_save_path, You can redefine the directory.
*/
$ Sesssavepath = dirname (_ file _). "/../data/sessions /";
If (is_writeable ($ sesssavepath) & is_readable ($ sesssavepath) {session_save_path ($ sesssavepath );}
Session_start ();
$ Vstr = '';
For ($ I = 0; $ I <4; $ I ++) $ vstr. = chr (mt_rand (65,90 ));
If (function_exists ("imagecreate ")){
$ Ntime = time ();
If (empty ($ _ session ['verifycode _ la']) | empty ($ _ session ['verifycode']) | ($ ntime-$ _ session ['verifycode _ la']> 5 )){
$ _ Session ['verifycode'] = strtolower ($ vstr );
$ _ Session ['verifycode _ la'] = $ ntime;
}
$ Vstr = $ _ session ['verifycode'];
$ Vstrlen = strlen ($ vstr );
$ Img = imagecreate (50, 20 );
Imagecolorallocate ($ img, 255,255,255 );
$ Line1 = imagecolorallocate ($ img, 240,220,180 );
$ Line2 = imagecolorallocate ($ img, 250,250,170 );
For ($ j = 3; $ j <= 16; $ j = $ j + 3 ){
Imageline ($ img, 2, $ j, 48, $ j, $ line1 );
}
For ($ j = 2; $ j <52; $ j = $ j + (mt_rand (3,6 ))){
Imageline ($ img, $ j, 2, $ j-6, 18, $ line2 );
}
$ Bordercolor = imagecolorallocate ($ img, 0x99,0x99,0x99 );
Imagerectangle ($ img, 0, 0, 49, 19, $ bordercolor );
$ Fontcolor = imagecolorallocate ($ img, 48, 61, 50 );
For ($ I = 0; $ I <$ vstrlen; $ I ++ ){
$ Bc = mt_rand (0, 1 );
$ Vstr [$ I] = strtoupper ($ vstr [$ I]);
Imagestring ($ img, 5, $ I * 10 + 6, mt_rand (2, 4), $ vstr [$ I], $ fontcolor );
}

Header ("pragma: no-cachern ");
Header ("cache-control: no-cachern ");
Header ("expires: 0rn ");

If (function_exists ("imagejpeg ")){
Header ("content-type: image/policrn ");
Imagejpeg ($ img );
} Else {
Header ("content-type: image/pngrn ");
Imagepng ($ img );
}
Imagedestroy ($ img );
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.