php session_start nChars _SESSION randStr substr md5 uniqid nChars im

 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
<?php
session_start();
$nChars = 5;
$_SESSION['randStr'] = substr(md5(uniqid()), 0, $nChars);
$im = imageCreateFromJpeg('images/noise.jpg');
imageAntiAlias($im, true);
$red = imageColorAllocate($im, 255, 0, 0);
$x = 20;
$y = 30;
$delta_X = 40;
for ($i=0; $i < $nChars; $i++) {
$size = rand(18, 30);
$angle = -30 + rand(0,60);
imageTtfText($im, $size, $angle, $x, $y, $red, 'fonts/georgia.ttf', $_SESSION['randStr']{$i});
$x += $delta_X;
}
header('Content-Type: image/jpg');
imageJpeg($im,null,50);
session_start();
$result = '';
if(!isset($_SESSION['randStr'])){
$result = 'ВКЛЮЧИ ГРАФИКУ!';
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_SESSION['randStr']) && $_SESSION['randStr'] == $_POST['str']) {
$result = 'Хорошо';
}else{
$result = 'Плохо';
}
}
?>