Artykuły na każdy temat
[PHP] Matematyczny token/captcha
<?php
session_start();
if(!empty($_POST['token']))
{
if(trim($_POST['token']) == $_SESSION['token'])
{
echo 'Zna matematykę to niesamowite!';
}
else
{
echo 'Dyskalkulia!';
}
}
else
{
$operations = array('+', '-', '×', '÷');
$unknown_position = rand(0, 2);
$random_word = rand(0, 1);
$operation = rand(0, 3);
$values = random_equation($operation);
$_SESSION['token'] = $values[$unknown_position];
// echo '<pre>'.$values[0].' '.$operations[$operation].' '.$values[1].' = '.$values[2].'</pre>';
for($h = $i = 0; $h < 3; ++$h)
{
if($unknown_position == $h)
{
$form[$h * 2] = '<input name="token">';
}
else
{
$form[$h * 2] = ($i == $random_word ? mark_my_words($values[$h]) : $values[$h]);
++$i;
}
}
$form[1] = $operations[$operation];
$form[3] = '=';
// Bo implode()?
ksort($form);
echo '<form method="post">'.implode(' ', $form).'</form>';
}
function random_equation($operation)
{
switch($operation)
{
// Dodawanie
case 0:
$values[2] = rand(2, 100);
$values[1] = rand(1, ($values[2] - 1));
$values[0] = $values[2] - $values[1];
break;
// Odejmowanie
case 1:
$values[0] = rand(2, 100);
$values[1] = rand(1, ($values[0] - 1));
$values[2] = $values[0] - $values[1];
break;
// Mnożenie
case 2:
include './factors.php';
$values[0] = rand(1, 50);
// Unfair draw
$values[1] = $factors[$values[0]][array_rand($factors[$values[0]])];
$values[2] = $values[0] * $values[1];
break;
// Dzielenie
case 3:
include './dividers.php';
$values[0] = rand(2, 100);
$values[1] = $dividers[$values[0]][array_rand($dividers[$values[0]])];
$values[2] = $values[0] / $values[1];
break;
}
return $values;
}
// Skrócona wersja funkcji
function mark_my_words($number)
{
if($number == 100)
{
$value = 'sto';
}
else
{
$words[] = array('jeden', 'dwa', 'trzy', 'cztery', 'pięć', 'sześć', 'siedem', 'osiem', 'dziewięć');
$words[] = array('dziesięć', 'jedenaście', 'dwanaście', 'trzynaście', 'czternaście', 'piętnaście', 'szesnaście', 'siedemnaście', 'osiemnaście', 'dziewiętnaście');
$words[] = array('dziesięć', 'dwadzieścia', 'trzydzieści', 'czterdzieści', 'pięćdziesiąt', 'sześćdziesiąt', 'siedemdziesiąt', 'osiemdziesiąt', 'dziewięćdziesiąt');
if($number > 0 && $number < 10)
{
$value = $words[0][$number - 1];
}
else
{
$numbers = str_split($number);
if($number > 9 && $number < 20)
{
$value = $words[1][$numbers[1]];
}
else
{
$value = $words[2][$numbers[0] - 1];
if($numbers[1] !== '0')
{
$value .= ' '.$words[0][$numbers[1] - 1];
}
}
}
}
return $value;
}
?>
Plik factors.php:
<?php
$factors[1] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100);
$factors[2] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50);
$factors[3] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33);
$factors[4] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);
$factors[5] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
$factors[6] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
$factors[7] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
$factors[8] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$factors[9] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
$factors[10] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$factors[11] = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$factors[12] = array(1, 2, 3, 4, 5, 6, 7, 8);
$factors[13] = array(1, 2, 3, 4, 5, 6, 7);
$factors[14] = array(1, 2, 3, 4, 5, 6, 7);
$factors[15] = array(1, 2, 3, 4, 5, 6);
$factors[16] = array(1, 2, 3, 4, 5, 6);
$factors[17] = array(1, 2, 3, 4, 5);
$factors[18] = array(1, 2, 3, 4, 5);
$factors[19] = array(1, 2, 3, 4, 5);
$factors[20] = array(1, 2, 3, 4, 5);
$factors[21] = array(1, 2, 3, 4);
$factors[22] = array(1, 2, 3, 4);
$factors[23] = array(1, 2, 3, 4);
$factors[24] = array(1, 2, 3, 4);
$factors[25] = array(1, 2, 3, 4);
$factors[26] = array(1, 2, 3);
$factors[27] = array(1, 2, 3);
$factors[28] = array(1, 2, 3);
$factors[29] = array(1, 2, 3);
$factors[30] = array(1, 2, 3);
$factors[31] = array(1, 2, 3);
$factors[32] = array(1, 2, 3);
$factors[33] = array(1, 2, 3);
$factors[34] = array(1, 2);
$factors[35] = array(1, 2);
$factors[36] = array(1, 2);
$factors[37] = array(1, 2);
$factors[38] = array(1, 2);
$factors[39] = array(1, 2);
$factors[40] = array(1, 2);
$factors[41] = array(1, 2);
$factors[42] = array(1, 2);
$factors[43] = array(1, 2);
$factors[44] = array(1, 2);
$factors[45] = array(1, 2);
$factors[46] = array(1, 2);
$factors[47] = array(1, 2);
$factors[48] = array(1, 2);
$factors[49] = array(1, 2);
$factors[50] = array(1, 2);
?>
Plik dividers.php:
<?php
$dividers[2] = array(1, 2);
$dividers[3] = array(1, 3);
$dividers[4] = array(1, 2, 4);
$dividers[5] = array(1, 5);
$dividers[6] = array(1, 2, 3, 6);
$dividers[7] = array(1, 7);
$dividers[8] = array(1, 2, 4, 8);
$dividers[9] = array(1, 3, 9);
$dividers[10] = array(1, 2, 5, 10);
$dividers[11] = array(1, 11);
$dividers[12] = array(1, 2, 3, 4, 6, 12);
$dividers[13] = array(1, 13);
$dividers[14] = array(1, 2, 7, 14);
$dividers[15] = array(1, 3, 5, 15);
$dividers[16] = array(1, 2, 4, 8, 16);
$dividers[17] = array(1, 17);
$dividers[18] = array(1, 2, 3, 6, 9, 18);
$dividers[19] = array(1, 19);
$dividers[20] = array(1, 2, 4, 5, 10, 20);
$dividers[21] = array(1, 3, 7, 21);
$dividers[22] = array(1, 2, 11, 22);
$dividers[23] = array(1, 23);
$dividers[24] = array(1, 2, 3, 4, 6, 8, 12, 24);
$dividers[25] = array(1, 5, 25);
$dividers[26] = array(1, 2, 13, 26);
$dividers[27] = array(1, 3, 9, 27);
$dividers[28] = array(1, 2, 4, 7, 14, 28);
$dividers[29] = array(1, 29);
$dividers[30] = array(1, 2, 3, 5, 6, 10, 15, 30);
$dividers[31] = array(1, 31);
$dividers[32] = array(1, 2, 4, 8, 16, 32);
$dividers[33] = array(1, 3, 11, 33);
$dividers[34] = array(1, 2, 17, 34);
$dividers[35] = array(1, 5, 7, 35);
$dividers[36] = array(1, 2, 3, 4, 6, 9, 12, 18, 36);
$dividers[37] = array(1, 37);
$dividers[38] = array(1, 2, 19, 38);
$dividers[39] = array(1, 3, 13, 39);
$dividers[40] = array(1, 2, 4, 5, 8, 10, 20, 40);
$dividers[41] = array(1, 41);
$dividers[42] = array(1, 2, 3, 6, 7, 14, 21, 42);
$dividers[43] = array(1, 43);
$dividers[44] = array(1, 2, 4, 11, 22, 44);
$dividers[45] = array(1, 3, 5, 9, 15, 45);
$dividers[46] = array(1, 2, 23, 46);
$dividers[47] = array(1, 47);
$dividers[48] = array(1, 2, 3, 4, 6, 8, 12, 16, 24, 48);
$dividers[49] = array(1, 7, 49);
$dividers[50] = array(1, 2, 5, 10, 25, 50);
$dividers[51] = array(1, 3, 17, 51);
$dividers[52] = array(1, 2, 4, 13, 26, 52);
$dividers[53] = array(1, 53);
$dividers[54] = array(1, 2, 3, 6, 9, 18, 27, 54);
$dividers[55] = array(1, 5, 11, 55);
$dividers[56] = array(1, 2, 4, 7, 8, 14, 28, 56);
$dividers[57] = array(1, 3, 19, 57);
$dividers[58] = array(1, 2, 29, 58);
$dividers[59] = array(1, 59);
$dividers[60] = array(1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60);
$dividers[61] = array(1, 61);
$dividers[62] = array(1, 2, 31, 62);
$dividers[63] = array(1, 3, 7, 9, 21, 63);
$dividers[64] = array(1, 2, 4, 8, 16, 32, 64);
$dividers[65] = array(1, 5, 13, 65);
$dividers[66] = array(1, 2, 3, 6, 11, 22, 33, 66);
$dividers[67] = array(1, 67);
$dividers[68] = array(1, 2, 4, 17, 34, 68);
$dividers[69] = array(1, 3, 23, 69);
$dividers[70] = array(1, 2, 5, 7, 10, 14, 35, 70);
$dividers[71] = array(1, 71);
$dividers[72] = array(1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72);
$dividers[73] = array(1, 73);
$dividers[74] = array(1, 2, 37, 74);
$dividers[75] = array(1, 3, 5, 15, 25, 75);
$dividers[76] = array(1, 2, 4, 19, 38, 76);
$dividers[77] = array(1, 7, 11, 77);
$dividers[78] = array(1, 2, 3, 6, 13, 26, 39, 78);
$dividers[79] = array(1, 79);
$dividers[80] = array(1, 2, 4, 5, 8, 10, 16, 20, 40, 80);
$dividers[81] = array(1, 3, 9, 27, 81);
$dividers[82] = array(1, 2, 41, 82);
$dividers[83] = array(1, 83);
$dividers[84] = array(1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84);
$dividers[85] = array(1, 5, 17, 85);
$dividers[86] = array(1, 2, 43, 86);
$dividers[87] = array(1, 3, 29, 87);
$dividers[88] = array(1, 2, 4, 8, 11, 22, 44, 88);
$dividers[89] = array(1, 89);
$dividers[90] = array(1, 2, 3, 5, 6, 9, 10, 15, 18, 30, 45, 90);
$dividers[91] = array(1, 7, 13, 91);
$dividers[92] = array(1, 2, 4, 23, 46, 92);
$dividers[93] = array(1, 3, 31, 93);
$dividers[94] = array(1, 2, 47, 94);
$dividers[95] = array(1, 5, 19, 95);
$dividers[96] = array(1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 96);
$dividers[97] = array(1, 97);
$dividers[98] = array(1, 2, 7, 14, 49, 98);
$dividers[99] = array(1, 3, 9, 11, 33, 99);
$dividers[100] = array(1, 2, 4, 5, 10, 20, 25, 50, 100);
?>
Ciężko mi sobie wyobrazić sytuację, w której wymagane będzie wywołanie tego kodu więcej niż jeden raz wobec jednego zapytania przesyłanego z zewnątrz jednak gdyby miało to miejsce to można pokusić się o optymalizację chociażby instrukcji include wewnątrz funkcji. Dodatkowo można uatrakcyjnić przedstawianie zagadek matematycznych korzystając chociażby z tego. Można również dodać możliwość przetwarzania tego, co zostało przesłane, jeżeli przyszło w formie słownej.
Komentarze
Dodaj komentarz
CapaciousCore