search
HomeBackend DevelopmentPHP TutorialA piece of code for php to generate random codes

  1. /*

  2. * Description: Generate random codes and display the random codes graphically.
  3. */
  4. $ViewRandomCode = mt_rand(1000,10000);

  5. session_start();

  6. $_SESSION['checksum'] = $ViewRandomCode;
  7. function set_4pixel($r, $g, $b, $x, $y)

  8. {
  9. global $sx, $sy, $pixels;
  10. $ofs = 3 * ($sx * $y + $x);

  11. $pixels[$ofs] = chr($r);
  12. $pixels[$ofs + 1] = chr($g);
  13. $pixels[$ofs + 2] = chr($b);
  14. $pixels[$ofs + 3] = chr($r);
  15. $pixels[$ofs + 4] = chr($g);
  16. $pixels[$ofs + 5] = chr($b);
  17. $ofs += 3 * $sx;
  18. $pixels[$ofs] = chr($r);
  19. $pixels[$ofs + 1] = chr($g);
  20. $pixels[$ofs + 2] = chr($b);
  21. $pixels[$ofs + 3] = chr($r);
  22. $pixels[$ofs + 4] = chr($g);
  23. $pixels[$ofs + 5] = chr($b);
  24. }
  25. //生成数字图象的函数
  26. function draw2digits($x, $y, $number)
  27. {
  28. draw_digit($x, $y, (int) ($number / 10));
  29. draw_digit($x + 11, $y, $number % 10);
  30. }
  31. function draw_digit($x, $y, $digit)

  32. {
  33. global $sx, $sy, $pixels, $digits, $lines;
  34. $digit = $digits[$digit];

  35. $m = 8;
  36. for ($b = 1, $i = 0; $i {
  37. if (($b & $digit) == $b) {
  38. $j = $i * 4;
  39. $x0 = $lines[$j] * $m + $x;
  40. $y0 = $lines[$j + 1] * $m + $y;
  41. $x1 = $lines[$j + 2] * $m + $x;
  42. $y1 = $lines[$j + 3] * $m + $y;
  43. if ($x0 == $x1) {
  44. $ofs = 3 * ($sx * $y0 + $x0);
  45. for ($h = $y0; $h $pixels[$ofs] = chr(0);
  46. $pixels[$ofs + 1] = chr(0);
  47. $pixels[$ofs + 2] = chr(0);
  48. }
  49. } else {
  50. $ofs = 3 * ($sx * $y0 + $x0);
  51. for ($w = $x0; $w $pixels[$ofs++] = chr(0);
  52. $pixels[$ofs++] = chr(0);
  53. $pixels[$ofs++] = chr(0);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. //将文字加入到图象中

  60. function add_chunk($type)
  61. {
  62. global $result, $data, $chunk, $crc_table;
  63. // chunk :为层

  64. // length: 4 字节: 用来计算 chunk
  65. // chunk type: 4 字节
  66. // chunk data: length bytes
  67. // CRC: 4 字节: 循环冗余码校验
  68. // copy data and create CRC checksum

  69. $len = strlen($data);
  70. $chunk = pack(“c*”, ($len >> 24) & 255,
  71. ($len >> 16) & 255,
  72. ($len >> 8) & 255,
  73. $len & 255);
  74. $chunk .= $type;
  75. $chunk .= $data;
  76. // calculate a CRC checksum with the bytes chunk[4..len-1]

  77. $z = 16777215;
  78. $z |= 255 $c = $z;
  79. for ($n = 4; $n $c8 = ($c >> 8) & 0xffffff;
  80. $c = $crc_table[($c ^ ord($chunk[$n])) & 0xff] ^ $c8;
  81. }
  82. $crc = $c ^ $z;
  83. $chunk .= chr(($crc >> 24) & 255);

  84. $chunk .= chr(($crc >> 16) & 255);
  85. $chunk .= chr(($crc >> 8) & 255);
  86. $chunk .= chr($crc & 255);
  87. // 将结果加到$result中

  88. $result .= $chunk;
  89. }
  90. //主程序

  91. $sx = 55;

  92. $sy = 21;
  93. $pixels = rand(100,99990)%2?”qwer”:”";
  94. // 填充

  95. for ($h = 0; $h {
  96. for ($w = 0; $w {
  97. $r = 100 / $sx * $w + 155;
  98. $g = 100 / $sy * $h + 155;
  99. $b = 255 – (100 / ($sx + $sy) * ($w + $h));
  100. $pixels .= chr($r);
  101. $pixels .= chr($g);
  102. $pixels .= chr($b);
  103. }
  104. }
  105. $checknum = isset($ViewRandomCode)?$ViewRandomCode:1234;

  106. $h = (int)($checknum/100);
  107. $m = (int)($checknum%100);
  108. $digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
  109. $lines = array(1, 1, 1, 2, 0, 1, 0, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 0, 1, 0);
  110. draw2digits(4, 2, $h);

  111. draw2digits(30, 2, $m);
  112. // set_4pixel(0, 0, 0, 26, 7);

  113. // set_4pixel(0, 0, 0, 26, 13);
  114. // 创建循环冗余码校验表

  115. $z = -306674912; // = 0xedb88320
  116. for ($n = 0; $n $c = $n;
  117. for ($k = 0; $k $c2 = ($c >> 1) & 0×7fffffff;
  118. if ($c & 1) $c = $z ^ ($c2); else $c = $c2;
  119. }
  120. $crc_table[$n] = $c;
  121. }
  122. // PNG file signature

  123. $result = pack(“c*”, 137,80,78,71,13,10,26,10);
  124. // IHDR chunk data:

  125. // width: 4 bytes
  126. // height: 4 bytes
  127. // bit depth: 1 byte (8 bits per RGB value)
  128. // color type: 1 byte (2 = RGB)
  129. // compression method: 1 byte (0 = deflate/inflate)
  130. // filter method: 1 byte (0 = adaptive filtering)
  131. // interlace method: 1 byte (0 = no interlace)
  132. $data = pack(“c*”, ($sx >> 24) & 255,
  133. ($sx >> 16) & 255,
  134. ($sx >> 8) & 255,
  135. $sx & 255,
  136. ($sy >> 24) & 255,
  137. ($sy >> 16) & 255,
  138. ($sy >> 8) & 255,
  139. $sy & 255,
  140. 8,
  141. 2,
  142. 0,
  143. 0,
  144. 0);
  145. add_chunk(“IHDR”);
  146. // 以下不敢乱翻译,请自行参考

  147. // scanline:
  148. // filter byte: 0 = none
  149. // RGB bytes for the line
  150. // the scanline is compressed with “zlib”, method 8 (RFC-1950):
  151. // compression method/flags code: 1 byte ($78 = method 8, 32k window)
  152. // additional flags/check bits: 1 byte ($01: FCHECK = 1, FDICT = 0, FLEVEL = 0)
  153. // compressed data blocks: n bytes
  154. // one block (RFC-1951):
  155. // bit 0: BFINAL: 1 for the last block
  156. // bit 1 and 2: BTYPE: 0 for no compression
  157. // next 2 bytes: LEN (LSB first)
  158. // next 2 bytes: one’s complement of LEN
  159. // LEN bytes uncompressed data
  160. // check value: 4 bytes (Adler-32 checksum of the uncompressed data)
  161. //
  162. $len = ($sx * 3 + 1) * $sy;
  163. $data = pack(“c*”, 0×78, 0×01,
  164. 1,
  165. $len & 255,
  166. ($len >> 8) & 255,
  167. 255 – ($len & 255),
  168. 255 – (($len >> 8) & 255));
  169. $start = strlen($data);
  170. $i2 = 0;
  171. for ($h = 0; $h $data .= chr(0);
  172. for ($w = 0; $w $data .= $pixels[$i2++];
  173. }
  174. }
  175. // calculate a Adler32 checksum with the bytes data[start..len-1]

  176. $s1 = 1;
  177. $s2 = 0;
  178. for ($n = $start; $n $s1 = ($s1 + ord($data[$n])) % 65521;
  179. $s2 = ($s2 + $s1) % 65521;
  180. }
  181. $adler = ($s2
  182. $data .= chr(($adler >> 24) & 255);

  183. $data .= chr(($adler >> 16) & 255);
  184. $data .= chr(($adler >> 8) & 255);
  185. $data .= chr($adler & 255);
  186. add_chunk(“IDAT”);
  187. // IEND: marks the end of the PNG-file

  188. $data = “”;
  189. add_chunk(“IEND”);
  190. // 列印图象

  191. echo($result);

  192. ?>
复制代码


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)