Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Opóżnione Captcha
Forum PHP.pl > Forum > PHP
Wilu88
Witam

Korzystam z klasy Auto generate CAPTCHA ze strony link w moim przypadku wygląda ona następująco:

plik captcha.inc.php

  1. <?php
  2. class captcha {
  3. protected $code;
  4. private $width = 150;
  5. private $height = 35;
  6. private $length = 6;
  7.  
  8. public function __construct($width = '', $height= '', $length = '') {
  9. if(intval($height) > 0){
  10. $this->height = $height;
  11. }
  12. if(intval($width) > 0){
  13. $this->width = $width;
  14. }
  15. if(intval($length) > 0){
  16. $this->length = $length;
  17. }
  18. $this->code = substr(sha1(mt_rand()), 10, $this->length);
  19. }
  20.  
  21. public function getCaptchaCode(){
  22. return $this->code;
  23. }
  24.  
  25. public function showCaptchaImage() {
  26. $image = imagecreatetruecolor($this->width, $this->height);
  27.  
  28. $height= imagesx($image);
  29. $width = imagesy($image);
  30.  
  31. $black = imagecolorallocate($image, 0, 0, 0);
  32. $white = imagecolorallocate($image, 255, 255, 255);
  33. $red = imagecolorallocatealpha($image, 255, 0, 0, 75);
  34. $green = imagecolorallocatealpha($image, 0, 255, 0, 75);
  35. $blue = imagecolorallocatealpha($image, 0, 0, 255, 75);
  36.  
  37. imagefilledrectangle($image, 0, 0, $height, $width, $white);
  38.  
  39. for($i = 0; $i<4; $i++){
  40. imagefilledellipse($image, ceil(rand(5, ($this->width+10))), ceil(rand(0, ($this->height+10))), 30, 30, $red);
  41. imagefilledellipse($image, ceil(rand(5, ($this->width+10))), ceil(rand(0, ($this->height+10))), 30, 30, $green);
  42. imagefilledellipse($image, ceil(rand(5, ($this->width+10))), ceil(rand(0, ($this->height+10))), 30, 30, $blue);
  43. }
  44. imagefilledrectangle($image, 0, 0, $height, 0, $black);
  45. imagefilledrectangle($image, $height- 1, 0, $height- 1, $width - 1, $black);
  46. imagefilledrectangle($image, 0, 0, 0, $width - 1, $black);
  47. imagefilledrectangle($image, 0, $width - 1, $height, $width - 1, $black);
  48. imagestring($image, 6, intval(($height- (strlen($_SESSION['captcha']) * 9)) / 2), intval(($width - 15) / 2), $_SESSION['captcha'], $black);
  49. header('Content-type: image/jpeg');
  50. imagejpeg($image);
  51. imagedestroy($image);
  52.  
  53. }
  54. }
  55.  
  56. ?>



plik. captcha.php odpowiedzialny za generowanie obrazka
  1. <?php
  2.  
  3. include('captcha.inc.php');
  4. $captcha = new Captcha(100,50,6);
  5. $_SESSION['captcha'] = $captcha->getCaptchaCode();
  6. $captcha->showCaptchaImage();
  7.  
  8. ?>


i na koniec fragment tekstu odpowiedzialny za wyświetlanei captchy w formularzu + na czas testów wyświetlenie obok wartości przetrzymywanej w sesji captcha:

  1. <tr>
  2. <td width="80" align="right"><img src="lib/captcha.php"></td><td><?php echo $_SESSION['captcha'];?></td>
  3. </tr>


Efekt jest taki że wartość wyświetlana w sesji jest kodem poprzedniego obrazka :/

Macie jakiś pomysł ?
potreb
No okey, ale po przepisaniu znaków captchy i wysłaniu formularza czyścisz sesją captchy?
Wilu88
Jak się okazało powyższy kod działa bez zarzutu podczas porównywania zawartości sesji cache z wpisywanym captcha. Także temat do zamknięcia wink.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.