Witam,

mam problem z sesjami i captchą, rozwiązanie które prezentuje na dole strony działało kiedyś teraz przestało. Nie wiem czy coś zmieniłem, czy to mój błąd, czy coś się posypało, dlatego proszę Was o pomoc.


Klasa odpowiedzialna za malowanie captcha:

  1. <?php
  2.  
  3. class Captcha{
  4.    
  5.    private $code;
  6.    private $length;
  7.    private $width;
  8.    private $height;
  9.    
  10.    public function __construct($length,$width,$height){
  11.        $this->length = $length;
  12.        $this->width = $width;
  13.        $this->height = $height;
  14.    }
  15.    
  16.    private function random_string($length){
  17.        $string = '';
  18.      for($i=0;$i<$length;$i++){
  19.          $string .= rand(0,9);
  20.      }
  21.      return($string);
  22.    }
  23.    
  24.    public function generateCode(){
  25.        $this->code = $this->random_string($this->length);
  26.        $_SESSION['ccode'] = $this->code;
  27.    }
  28.    
  29.    public function showCaptcha(){
  30.        
  31.  
  32.    header("Content-type: image/gif");
  33.  
  34.    $im = imagecreatetruecolor($this->width, $this->height);
  35.    
  36.    $white = imagecolorallocate($im, 255, 255, 255);
  37.    $grey = imagecolorallocate($im, 128, 128, 128);
  38.    $black = imagecolorallocate($im, 0, 0, 0);
  39.    imagefilledrectangle($im, 0, 0, $this->width, $this->height, $white);
  40.    
  41.  
  42.    $font = '../_RES/fonts/actionj.ttf';
  43.  
  44.    imagettftext($im, 20, 5, 11, 30, $grey, $font, $this->code);
  45.    
  46.  
  47.    imagettftext($im, 20, 5, 10, 31, $black, $font, $this->code);
  48.    
  49.  
  50.    imagegif($im);
  51.    imagedestroy($im);
  52.  
  53.    }
  54. }
  55.  
  56. ?>


Tutaj mały PHP który jest mi pomocny przy wyświetlaniu captcha - get_code.php:

  1. <?php
  2.  
  3. require_once '../class/validators/captcha.php';
  4. $captcha = new Captcha(5,100,30);
  5. $captcha->generateCode();
  6. $captcha->showCaptcha();
  7.  
  8. ?>


Oraz plik testowy:

  1. <?php
  2.  
  3. ?>
  4. <img src="helpers/get_code.php" alt="scode">
  5. <?php
  6.  
  7. echo "c: ".$_SESSION["ccode"];
  8. ?>


Efekt jaki chciałem uzyskać to standardowy, czyli generuję sobie captcha a po odświeżeniu strony zmienna $_SESSION["ccode"], powinna mieć wartość taką jaką widziałem na captchy. Błąd pewnie jest błahy, gdzie go popełniłem ? Oraz dlaczego jak w metodzie generateCode umieszczę licznik (na sesjach) to będzie on skakał co 2 ? Czy teraz 2 razy się ta metoda wywołuje raz do generowania a raz do wyświetlania ? Kompletnie już zgłupiałem.