Project

General

Profile

« Previous | Next » 

Revision 227

Added by ryan about 19 years ago

Finished correct Captcha implementation in form mod

View differences:

captcha.php
32 32

  
33 33
*/
34 34

  
35
$image = imagecreate(120, 30);
35
require_once("../config.php");
36 36

  
37
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
38
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
39
$darkgray = imagecolorallocate($image, 0x50, 0x50, 0x50);
40

  
41
srand((double)microtime()*1000000);
42

  
43
for ($i = 0; $i < 30; $i++) {
44
  $x1 = rand(0,120);
45
  $y1 = rand(0,30);
46
  $x2 = rand(0,120);
47
  $y2 = rand(0,30);
48
  imageline($image, $x1, $y1, $x2, $y2 , $gray);  
37
if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg') AND isset($_SESSION['captcha'])) {
38
	
39
	$image = imagecreate(120, 30);
40
	
41
	$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
42
	$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
43
	$darkgray = imagecolorallocate($image, 0x50, 0x50, 0x50);
44
	
45
	srand((double)microtime()*1000000);
46
	
47
	for($i = 0; $i < 30; $i++) {
48
		$x1 = rand(0,120);
49
		$y1 = rand(0,30);
50
		$x2 = rand(0,120);
51
		$y2 = rand(0,30);
52
		imageline($image, $x1, $y1, $x2, $y2 , $gray);  
53
	}
54
	
55
	for ($i = 0; $i < 5; $i++) {
56
		$fnt = rand(3,5);
57
		$x = $x + rand(12 , 20);
58
		$y = rand(7 , 12); 
59
		imagestring($image, $fnt, $x, $y, substr($_SESSION['captcha'], $i, 1), $darkgray); 
60
	}
61
	
62
	header('Content-type: image/png');
63
	imagepng($image);
64
	imagedestroy($image);
65
	
49 66
}
50 67

  
51
for ($i = 0; $i < 5; $i++) {
52
$cnum[$i] = rand(0,9);
53
}
54

  
55
for ($i = 0; $i < 5; $i++) {
56
 $fnt = rand(3,5);
57
 $x = $x + rand(12 , 20);
58
 $y = rand(7 , 12); 
59
 imagestring($image, $fnt, $x, $y, $cnum[$i] , $darkgray); 
60
}
61

  
62
$digit = "$cnum[0]$cnum[1]$cnum[2]$cnum[3]$cnum[4]";
63

  
64
session_start();
65
$_SESSION['captcha'] = $digit;
66

  
67
header('Content-type: image/png');
68
imagepng($image);
69
imagedestroy($image);
70

  
71 68
?>

Also available in: Unified diff