PHP generated verification code examples!

Today spent some time studying how to use PHP generated verification code. Mainly the study of GD library of image processing functions. As long as familiar with these functions, using PHP to operate the image was quite convenient. Here I write the code generated verification code examples, notes written on each step, and related functions please refer to manual. The final result was good o (∩ _ ∩) o. .. If you have a better idea, please share with me!
PHP Code

   1. <? Php
   2. //-------------------------------------
   3. / / File Description: Generate Code
   4. / / File of: Jesse Lee
   5. / / On the home page: http://www.lisijie.com.cn
   6. / / Last updated :2008-4-27
   7. //-------------------------------------
   8.
   9. Session_start ();
  10. / / Does not exist imageCreate function is that the current environment does not support the GD library
  11. If (function_exists ('imagecreate')) (
  12. / / Generate random 4-character string as the verification code
  13. $ Str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  14. $ Code = array ();
  15. For ($ i = 0; $ i <4; $ i + +) (
  16. $ Code [] = $ str [mt_rand (0, strlen ($ str) -1)];
  17.)
  18. / / To verify the code is written to the Session, ignoring case
  19. $ _SESSION ['Vdcode'] = strtolower (implode ('',$ code));
  20.
  21. $ Width = 50; / / image width
  22. $ Height = 20; / / image height
  23.
  24. $ Im = ImageCreate ($ width, $ height); / / create graphics
  25. ImageColorAllocate ($ im, 255,255,255); / / fill the background color is white
  26.
  27. / / Graphics add to the mottled with pale
  28. For ($ i = 0; $ i <100; $ i + +) (
  29. $ Pxcolor = ImageColorAllocate ($ im, mt_rand (100,255), mt_rand (100,255), mt_rand (100,255));
  30. ImageSetPixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pxcolor);
  31.)
  32.
  33. / / Draw the border with dark colors
  34. $ Bordercolor = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100));
  35. ImageRectangle ($ im, 0,0, $ width-1, $ height-1, $ bordercolor);
  36.
  37. / / Use of the more obvious the text color to write validation code
  38. $ Offset = 5;
  39. Foreach ($ code as $ char) (
  40. $ Textcolor = ImageColorAllocate ($ im, mt_rand (0,250), mt_rand (0,150), mt_rand (0,250));
  41. ImageChar ($ im, 5, $ offset, 2, $ char, $ textcolor);
  42. $ Offset + = 10;
  43.)
  44.
  45. / / No cache
  46. Header ("pragma: no-cache \ r \ n");
  47. Header ("Cache-Control: no-cache \ r \ n");
  48. Header ("Expires: 0 \ r \ n");
  49. / / Check the system to support the file type, priority for the PNG-> JPEG-> GIF
  50. If (ImageTypes () & IMG_PNG) (
  51. Header ('Content-Type: image / png');
  52. ImagePNG ($ im);
  53.) Elseif (ImageTypes () & IMG_JPEG) (
  54. Header ('Content-Type: image / jpeg');
  55. ImageJPEG ($ im);
  56.) Else (
  57. Header ('Content-Type: image / gif');
  58. ImageGif ($ im);
  59.)
  60.) Else (
  61. / / Do not support the GD library, then the output of the default authentication code ABCD
  62. $ _SESSION ['Vdcode'] = 'abcd';
  63. Header ('Content-Type: image / jpeg');
  64. $ Fp = fopen ('vdcode.jpg', 'rb');
  65. Echo fread ($ fp, filesize ('vdcode.jpg'));
  66. Fclose ($ fp);
  67.)
  68.?>

Does not support the GD library where the output of the picture

Declined comment