📜 ⬆️ ⬇️

How I did the captcha

Once back in 2009, I made a website for musicians. The only means of feedback on the site was a guest book. In order not to let bots, spammers and bad people need a captcha. As a site for musicians, I thought, let the captcha be a music too.

What turned out looks like this:


or so:


')
Notes need to recognize and select the appropriate in the four (by the number of notes) drop-down lists:


Alterations had to be abandoned, since it would be too difficult for me (at that time I couldn’t figure out how to do it), or for users, because the input process would be complicated at times.

On the server, a picture is generated from (pseudo) randomly selected four pictures of notes and pauses. The names of the pictures - numbers from 0 to 13 + extension (png) - correspond to the position number of the note on the musical staff, starting from the bottom incremental. The key is also random - bass or treble. To understand what the note means in the current key, the following converter was made:

<?php function scrip_href($x){ switch( $x ) { case 0: $x1=0; break; case 1: $x1=1; break; case 2: $x1=2; break; case 3: $x1=3; break; case 4: $x1=4; break; case 5: $x1=5; break; case 6: $x1=6; break; case 7: $x1=7; break; case 8: $x1=1; break; case 9: $x1=2; break; case 10: $x1=3; break; case 11: $x1=4; break; case 12: $x1=5; break; case 13: $x1=6; break; } return $x1; } function bass_href($x){ switch( $x ) { case 0: $x1 = 0; break; case 1: $x1 = 3; break; case 2: $x1 = 4; break; case 3: $x1 = 5; break; case 4: $x1 = 6; break; case 5: $x1 = 7; break; case 6: $x1 = 1; break; case 7: $x1 = 2; break; case 8: $x1 = 3; break; case 9: $x1 = 4; break; case 10: $x1 = 5; break; case 11: $x1 = 6; break; case 12: $x1 = 7; break; case 13: $x1 = 1; break; } return $x1; } ?> 


Then everything is simple - we generate the key and notes:
 <?php mt_srand( time() + (double)microtime()*55 ); $n = mt_rand(1, 2); $n1 = mt_rand(0, 13); $n2 = mt_rand(0, 13); $n3 = mt_rand(0, 13); $n4 = mt_rand(0, 13); ?> 


We convert what came out into something understandable:
 <?php if( $n == 1 ){ $x1 = scrip_href($n1); $x2 = scrip_href($n2); $x3 = scrip_href($n3); $x4 = scrip_href($n4); } elseif( $n == 2 ){ $x1 = bass_href($n1); $x2 = bass_href($n2); $x3 = bass_href($n3); $x4 = bass_href($n4); } ?> 


Then it remains only to write the resulting code in the session, glue and give the picture.

Thus, in the session we have recorded real numbers of notes (pause = 0, up to = 1, re = 2, etc.), and the user sees a funny captcha.

Thank you for reading to the end.

Source: https://habr.com/ru/post/150224/


All Articles