A stable php login system verification code

Source: Internet
Author: User
A stable php login system verification code

  1. Session_start ();
  2. $ Type = 'GIF ';
  3. $ Width = 56;
  4. $ Height = 22;
  5. Header ("Content-type: image/". $ type );
  6. Srand (double) microtime () * 1000000 );
  7. $ Randval = randStr (4, "NUMBER ");
  8. If ($ type! = 'GIF' & function_exists ('imagecreatetruecolor ')){
  9. $ Im = @ imagecreatetruecolor ($ width, $ height );
  10. } Else {
  11. $ Im = @ imagecreate ($ width, $ height );
  12. }
  13. $ R = Array (225,211,255,223 );
  14. $ G = Array (225,236,237,215 );
  15. $ B = Array (225,236,166,125 );
  16. $ Key = rand (0, 3 );
  17. $ BackColor = ImageColorAllocate ($ im, $ r [$ key], $ g [$ key], $ B [$ key]); // background color (random)
  18. $ BorderColor = ImageColorAllocate ($ im, 0, 0, 0); // border color
  19. $ PointColor = ImageColorAllocate ($ im, 255,170,255); // vertex color
  20. @ Imagefilledrectangle ($ im, 0, 0, $ width-1, $ height-1, $ backColor); // background position
  21. @ Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ borderColor); // border position
  22. $ StringColor = ImageColorAllocate ($ im, 153 );
  23. For ($ I = 0; $ I <= 100; $ I ++ ){
  24. $ PointX = rand (2, $ width-2 );
  25. $ PointY = rand (2, $ height-2 );
  26. @ Imagesetpixel ($ im, $ pointX, $ pointY, $ pointColor );
  27. }
  28. @ Imagestring ($ im, 16, 10, 2, $ randval, $ stringColor );
  29. $ ImageFun = 'image'. $ type;
  30. $ ImageFun ($ im );
  31. @ ImageDestroy ($ im );
  32. $ _ SESSION ['validatecode'] = $ randval;
  33. // Generate a random string
  34. Function randStr ($ len = 6, $ format = 'all '){
  35. Switch ($ format ){
  36. Case 'all ':
  37. $ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 '; break;
  38. Case 'char ':
  39. $ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'; break;
  40. Case 'number ':
  41. $ Chars = '20140901'; break;
  42. Default:
  43. $ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 ';
  44. Break;
  45. }
  46. $ String = "";
  47. While (strlen ($ string) <$ len)
  48. $ String. = substr ($ chars, (mt_rand () % strlen ($ chars), 1 );
  49. Return $ string;
  50. }
  51. ?>

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.