- Session_start ();
- // A string that randomly generates the verification code
- Function random ($ len ){
- $ Srcstr = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789 ";
- Mt_srand ();
- $ Strs = "";
- For ($ I = 0; $ I <$ len; $ I ++ ){
- $ Strs. = $ srcstr [mt_rand (0, 33)];
- }
- Return ($ strs );
- }
- $ Str = random (5); // randomly generated string
- $ Width = 60; // the image width of the verification code
- $ Height = 25; // height of the verification code Image
- // Date in the past
- Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
- // Always modified
- Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
- // HTTP/1.1
- Header ("Cache-Control: no-store, no-cache, must-revalidate ");
- Header ("Cache-Control: post-check = 0, pre-check = 0", false );
- // HTTP/1.0
- Header ("Pragma: no-cache ");
- Header ("Content-Type: image/png ");
- $ Im = imagecreate ($ width, $ height );
- $ Back = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF); // background color
- $ Pix = imagecolorallocate ($ im, 187,190,247); // blur the dot color
- $ Font = imagecolorallocate ($ im, 41,163,238); // font color
- // Draw 1000 blur points
- Mt_srand ();
- For ($ I = 0; I I <1000; $ I ++ ){
- Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pix );
- }
- Imagestring ($ im, 5, 7, 5, $ str, $ font); // draw a random string
- Imagerectangle ($ im, $ width-1, $ height-1, $ font); // draw a 1px border around the verification code Image
- Imagepng ($ im); // Create a PNG image
- Imagedestroy ($ im); // deconstruct the image handle and release it to the memory space.
- $ _ SESSION ["auth_code"] = $ str;
- ?>
Example 2: php random verification code php generates a simple verification code
- $ Image_width = 140;
- $ Image_height = 50;
- Srand (microtime () * 10000 );
- For ($ I = 0; $ I <4; $ I ++ ){
- $ Number. = dechex (rand (0, 15 ));
- }
- $ Check = $ number;
- $ Im = imagecreate ($ image_width, $ image_height );
- Imagecolorallocate ($ im, 255,255,255 );
- For ($ I = 0; $ I
- $ Font = rand (40, 80 );
- $ X = rand (1, 8) + $ image_width * $ I/4;
- $ Y = rand (1, $ image_height/4 );
- $ Color = imagecolorallocate ($ im, rand (0,100), rand (0,150), rand (0,200 ));
- Imagestring ($ im, $ font, $ x, $ y, $ check [$ I], $ color );
- }
- Imagepng ($ im );
- Imagedestroy ($ im );
- ?>
|