login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Beveiliging > Captcha class [Simple 2 Use]

Captcha class [Simple 2 Use]

Auteur: ArieMedia - 09 april 2009 - 16:07 - Gekeurd door: Koen - Hits: 5661 - Aantal punten: 3.00 (1 stem)



Installatie
plaats mkimg.php ergens op je webserver en roep deze aan.
In dezelfde map moeten je fonts komen, en je map moet CHMOD 777 hebben voor de afbeelding.

Vergeet niet om in mkimg.php in de functie font, de fonts te declareren!
en wees er zeker van dat je ob_start(); en session_start(); gebruikt hebt!
Het gebruik kan zo:
  1. <?php
  2. include('mkimg.php');
  3. ?>
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  8. <title>"Captcha" by Arie2Zero</title>
  9. </head>
  10. <body>
  11. <?php
  12. $captcha = new secureForm;
  13. if(isset($_POST['control'])) {
  14. if($captcha->checkcode($_POST['ncode']) == true) {
  15. echo '<h3>Code was goed</h3>';
  16. } else { echo '<h3>Code was verkeerd</h3>'; $captcha->secureForm($captcha->code(6)); } // secureForm(code, width, height, maxlines)
  17. } else { $captcha->secureForm($captcha->code(6)); }
  18.  
  19. echo '<img src="SecuredByArie.png" alt="Secured" /><br>
  20. <p style="font-size: 10px;">De code bevat geen 0</p>';
  21. echo '<p>Controle:<br>
  22. <form method="post" action="index.php">
  23. Toets code in om verder te gaan: <input type="text" name="ncode" /><br>
  24. <input type="submit" name="control" value="Go On" />
  25. </form></p>';
  26. ?>

Code:
  1. <?php
  2. /*
  3. THIS CLASS WILL CREATE A CAPTCHA, MAKE SURE YOU STARTED A SESSION.
  4. AUTHOR: ARJAN VELDKAMP [ARIE2ZERO.NL]
  5. VERSION: 0.7
  6. RELEASED ON 29 APRIL 2009
  7.  
  8. Use secureform like this way to create a new image:
  9. $captcha = new secureForm;
  10. $captcha->secureForm($captcha->code(4), 'mycaptcha', 200, 40, 8);
  11.  
  12. it will make a new image and a session with a random code with the width of 200px, height of 40px and
  13. between 4 and 8 lines with the name mycaptcha.png
  14.  
  15. make sure that the map wich contains this class has rights to write (CHMOD 777)
  16. */
  17. class secureForm {
  18.  
  19. protected $code;
  20. protected $ucode;
  21. protected $width;
  22. protected $height;
  23. protected $image;
  24. protected $lines;
  25. protected $naam;
  26.  
  27. /**
  28. * Constructor, will define all information this class needs
  29. * input string, string, int, int, int
  30. **/
  31. public function secureForm($code='Code', $naam='captcha', $width=100, $height=30, $mlines=8) {
  32. $this->code = str_split($code);
  33. $this->ucode = $code;
  34. $this->width = $width;
  35. $this->height = $height;
  36. $this->lines = $mlines;
  37. $this->naam = $naam;
  38. $this->image = $this->image();
  39. }
  40.  
  41. /**
  42. * This function will create the image based on imagesize and characters,
  43. * it wil automaticly choose his position, pick a color and place dots and lines
  44. **/
  45. private function image() {
  46. $imgH = $this->height - 20;
  47. $img = imagecreatetruecolor($this->width, $this->height);
  48.  
  49. $wit = imagecolorallocate($img, 255, 255, 255);
  50. imagefill($img, 0, 0, $wit); // the background color
  51. // add some dots.. because they are funny to..
  52. $dots = 200;
  53. for($i=0; $i<$dots; $i++) {
  54. $red = rand(0, 200);
  55. $green = rand(0, 200);
  56. $blue = rand(0, 200);
  57. $x = rand(0, $this->width);
  58. $y = rand(0, $this->height);
  59. $kleur = imagecolorallocate($img, $red, $green, $blue);
  60. imagesetpixel($img, $x, $y, $kleur);
  61. }
  62.  
  63. $pos = array();
  64. // make the image variable
  65. for($i=0; $i < count($this->code); $i++) {
  66. // pick some colors
  67. $red = rand(0, 200);
  68. $green = rand(0, 200);
  69. $blue = rand(0, 200);
  70. if($i == 0) { $edge = rand(0, -20); } else { $edge = rand(-30, 50); }
  71. $fColor = imagecolorallocate($img, $red, $green, $blue);
  72. // define some areas to put the letters on
  73. $pos[$i] = array('width' => $this->fontspace() *$i + 1, // position x
  74. 'height' => rand(20, $imgH), // position y
  75. 'edge' => $edge); // the edge
  76. $imgWN = $this->fontspace() * $i;
  77. imagettftext($img, 14, $pos[$i]['edge'], $pos[$i]['width'], $pos[$i]['height'], $fColor, $this->font(), $this->code[$i]);
  78. }
  79. // and now add some lines, just for the fun =D
  80. $lines = rand(2, $this->lines);
  81. for($i=0; $i<$lines; $i++) {
  82. $red = rand(0, 200);
  83. $green = rand(0, 200);
  84. $blue = rand(0, 200);
  85. $x1 = rand(4, $this->width -30);
  86. $y1 = rand(4, $this->height - 20);
  87. $x2 = rand($x1, $this->width);
  88. $y2 = rand($y1, $this->height);
  89. $kleur = imagecolorallocate($img, $red, $green, $blue);
  90. imageline($img, $x1, $y1, $x2, $y2, $kleur);
  91. }
  92. imagepng ($img, $this->naam.'.png');
  93.  
  94. imagedestroy($img);
  95. }
  96. /**
  97. * This wil calculate the spacing between the letters
  98. **/
  99. private function fontspace() {
  100. $space = round($this->width / count($this->code), 0);
  101. return $space;
  102. }
  103.  
  104. /**
  105. * Define all your fonts here, you have to put them in the same folder as mkimg.php
  106. **/
  107. private function font() {
  108. $font = array(); // Define all your possible fonts here
  109. $font[] = 'Market_Deco.ttf';
  110. $font[] = 'Mouse_Deco.ttf';
  111. $font[] = 'Fiesta.ttf';
  112. $aFont = count($font) - 1;
  113. $tFont = rand(0, $aFont);
  114.  
  115. return $font[$tFont];
  116. }
  117.  
  118. /**
  119. * This wil make a random code
  120. * input int / output string
  121. **/
  122. public function code($maxint) {
  123. $tekens = array_merge(range('A', 'Z'), range(1, 9));
  124. $teken = array();
  125. for($i=0; $i < $maxint; $i++) {
  126. $wTeken = rand(0, count($tekens) -1);
  127. $teken[] = $tekens[$wTeken];
  128. }
  129. $code = implode('', $teken);
  130. $_SESSION['code'] = $code;
  131. return $code;
  132. }
  133.  
  134. /**
  135. * Check of the posted code is even to the image
  136. * input string / output true or false
  137. **/
  138.  
  139.  
  140. public function checkcode($gepost) {
  141. if(strtoupper($gepost) == $_SESSION['code']) {
  142. return true;
  143. } else { return false; }
  144. }
  145.  
  146. }
  147.  
  148. ?>
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (2)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.053s