Refer to the http://beebole.com/blog/erlang/how-to-implement-captcha-in-erlang-web-application/, move to cowboy, nonsense not much to say, direct sticker code
Note that cowboy version 1.0.1 is required
Create a project
Rebar-creator Create-app Testcowboy
Testcowboy_app.erl
-module (Testcowboy_app).-behaviour (Application).-export ([START/2, STOP/1]).-define (c_acceptors, + ). Start ( _starttype, _startargs), Application:start (crypto), Application:start (cowlib), application: Start (ranch), Application:start (Cowboy), Routes = Route_helper:get_routes (), Dispatch = Cowboy_router:compile (Routes), port = 8080, transopts = [{port, port}], protoopts = [{env], [{ Dispatch, Dispatch}]}], cowboy:start_http (http,? C_acceptors, Transopts, protoopts). Stop (_state), OK.
Route_helper.erl
-Module(route_helper). -Export([get_routes/0] [ {' _ ', [ {'/captcha ', Captcha_ Handler, []} ]} ].
Captcha_handler.erl
-Module(Captcha_handler).-Export([INIT/3]).-Export([HANDLE/2]).-Export([TERMINATE/3]). Init (_transport, Req, [])-{OK, Req, Undefined}.handle (Req, State),%Codehex is used for authentication, it needs to be saved locally, capcode data submitted by the user%Mycaptcha:check (Codehex, Capcode) {codehex, binpng}=mycaptcha:new (), REQ2= Cowboy_req:set_resp_cookie (<< "cap" >>, Codehex, [{path, << "/" >>}], Req), {OK, Req3}= Cowboy_req:reply ([{<< "Content-type" >>, << "Image/png" >>}],binpng, Req2), {OK, Req3, state}.terminate (_reason, _req, _state)-OK.
Mycaptcha.erl
-Module(Mycaptcha).-compile (Export_all). New ()-Cryptkey= CaseEts:info (CAPTCHA) ofundefined-Captcha=ets:new (CAPTCHA, [Set, Public, named_table]), CK= Crypto:rand_bytes (16), true=Ets:insert (CAPTCHA, {ck}), CK; _info-Ets:first (CAPTCHA)End, FileName= Lists:flatmap ( Fun(item), Integer_to_list (item)End, Tuple_to_list (now ())), Code= Generate_rand (5), File= Io_lib:format ("/tmp/~s.png", [FileName]), CMD= Io_lib:format ("Convert-background ' None '-fill ' #222222 '-size 175-gravity center-wave 5x100-swirl 50-font DejaVu-S Erif-book-pointsize Label:~s-draw ' Bézier 10,40 50,35 100,35 150,35 200,50 250,35 300,35 ' ~s ', [Code, File]), Os:cmd (cmd), {OK, binpng}=file:read_file (file), File:delete (file), Sha= Crypto:sha_mac (Cryptkey, Integer_to_list (Lists:sum (Code)) + +Code), Codehex=Mochihex:to_hex (Sha), {codehex, Binpng}.check (Codehex, Code)-Sha=Mochihex:to_bin (Codehex), Cryptkey=Ets:first (CAPTCHA), CaseCrypto:sha_mac (Cryptkey, Integer_to_list (lists:sum (code)) + + Code) ofSha-true; _ -false End. Generate_rand (Length)- Now=Now (), Random:seed (Element (1, now), Element (2, now), Element (3, now)), LISTS:FOLDL ( Fun(_i, ACC), [Do_rand (0) | ACC]End, [], Lists:seq (1, Length)). Do_rand (R) whenR > G, r < 58; R > G, r < 91; R >R;do_rand (_r)%do_rand (+ random:uniform (74)).%%exluding ZeroDo_rand (+ random:uniform (75)).
Moche_hex.erl
Download it Yourself
Rebar.config
%-*-Erlang-*-{erl_opts, [Debug_info]}. {deps, [{Cowboy,".*", {git,"Https://github.com/ninenines/cowboy", {tag,"1.0.1"}}}]}. {cover_enabled,true}. {eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}. {Sub_dirs, ["Apps/testcowboy","rel"]}.
Test HTML
<img src="http://127.0.0.1:8080/captcha " width=" height=">"
Cowboy Add Verification Code