Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate Picture Verification Code by PHP

Shulou Source: shulou.com Published: 2022-06-02 10:46:01 09月25日 Update

This article introduces the relevant knowledge of "how to generate picture verification code by PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Five examples of PHP generating picture CAPTCHA, including numeric CAPTCHA, numeric + alphabetic CAPTCHA, Chinese CAPTCHA, imitating google CAPTCHA and arithmetic CAPTCHA. The principle of generating CAPTCHA by PHP: generate a picture with CAPTCHA through GD library, and save the CAPTCHA in Session.

Js verification

$(function () {("# getcode_num") .click (function () {/ / numeric verification $(this) .attr ("src", 'code_num.php?' + Math.random ());}); $("# chk_num") .click (function () {var code_num = $("# code_num") .val () $.post ("chk_code.php?act=num", {code: code_num}, function (msg) {if (msg = = 1) {alert ("CAPTCHA is correct!") ;} else {alert ("CAPTCHA error!") ;}});}); / / number + letter verification $("# getcode_char") .click (function () {$(this) .attr ("src", 'code_char.php?' + Math.random ());}) Click ("# chk_char") .click (function () {var code_char = $("# code_char") .val (); $.post ("chk_code.php?act=char", {code: code_char}, function (msg) {if (msg = = 1) {alert ("CAPTCHA is correct!") ;} else {alert ("CAPTCHA error!") ;}});}); / / Chinese verification code $("# getcode_zh") .click (function () {$(this) .attr ("src", 'code_zh.php?' + Math.random ());}) ("# chk_zh") .click (function () {var code_zh = escape ($("# code_zh"). Val ()); $.post ("chk_code.php?act=zh", {code: code_zh}, function (msg) {if (msg = = 1) {alert ("CAPTCHA is correct!") ;} else {alert ("CAPTCHA error!") ;}});}); / / google verification $("# getcode_gg") .click (function () {$(this) .attr ("src", 'code_gg.php?' + Math.random ());}); $("# chk_gg") .click (function () {var code_gg = $("# code_gg") .val () $.post ("chk_code.php?act=gg", {code: code_gg}, function (msg) {if (msg = = 1) {alert ("CAPTCHA is correct!") ;} else {alert ("CAPTCHA error!") ;}});}); / / arithmetic verification $("# getcode_math") .click (function () {$(this) .attr ("src", 'code_math.php?' + Math.random ();}); $("# chk_math") .click (function () {var code_math = $("# code_math") .val ()) $.post ("chk_code.php?act=math", {code: code_math}, function (msg) {if (msg = = 1) {alert ("CAPTCHA is correct!") ;} else {alert ("CAPTCHA error!") ;})

Generate CAPTCHA

Session_start (); getCode (4 num,$w,$h) {$code = ""; for ($I = 0; $I < $num; $iCAPTCHA +) {$code. = rand (0prime9);} / / 4-bit CAPTCHA can also be directly generated by rand (10009999) to write the generated CAPTCHA to session, using $_ SESSION ["helloweba_num"] = $code for verification / create a picture and define the color value header ("Content-type: image/PNG"); $im = imagecreate ($w, $h); $black = imagecolorallocate ($im, 0,0,0); $gray = imagecolorallocate ($im, 200,200,200); $bgcolor = imagecolorallocate ($im, 255,255,255); / / fill the background imagefill ($im, 0,0, $gray) / draw a border imagerectangle ($im, 0,0, $wmur1, $hmur1, $black); / / randomly draw two dotted lines to interfere with $style = array ($black,$black,$black,$black,$black, $gray,$gray,$gray,$gray,$gray); imagesetstyle ($im, $style); $y1 = rand (0, $h); $y2 = rand (0, $h) $y3 = rand (0, $h); $y4 = rand (0, $h); imageline ($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); imageline ($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); / / randomly generate a large number of black spots on the canvas, causing interference; for ($I = 0; $I < 80) Imagesetpixel ($im, rand (0, $w), rand (0, $h), $black);} / / numbers are randomly displayed on the canvas, and the horizontal spacing and position of characters are randomly generated by a certain range of fluctuations $strx = rand (3,8); for ($I = 0; $I < $num; $iTunes +) {$strpos = rand (1,6) Imagestring ($im, 5, $strx, $strpos, substr ($code, $I, 1), $black); $strx + = rand (8,12);} imagepng ($im); / / output picture imagedestroy ($im); / / release the memory occupied by the picture} "how to generate image verification code" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Verify generate picture number error function content letter more canvas knowledge arithmetic Chinese interference output practical learned next location memory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Shulou Tech Info Shulou Information Microsoft