1

Hello i am learning php now and developing website for my education. I am facing problem if i try to add captcha image. I don't know where is the problem but instead of working captcha i get blank page. I even tried few already done captchas from github but get same problem i think the problem could be with fonts but i am not sure "ts probably my stupidity and i am doing something wrong :D" . Anyway if anyone can help me with it it would be great. code i tried to use:

captcha.php

<?php
class captcha {
private static $captcha = "__captcha__";
public static $font;
private static $width = 70;
private static $height = 70;
private static $font_size = 40;
private static $character_width = 40;
private static function session_exists() {
    return isset($_SESSION);
}
private static function set_font() {

    self::$font = self::$captcha;

    $AnonymousClippings ='there is inserted chars from font you can';
  self::$font = tempnam(sys_get_temp_dir(), self::$captcha);

    $handle = fopen(self::$font,"w+");
    fwrite($handle,base64_decode($AnonymousClippings));
    fclose($handle);

    return self::$font;
}

private static function get_random() {

    $type = rand(0,2);

    switch($type) {
        case 2:
            $random = chr(rand(65,90));
        break;
        case 1:
            $random = chr(rand(97,122));
        break;
        default:
            $random = rand(0,9);
    }

    return $random;
}

private static function generate_code($length) {

    $code = null;
    for($i = 0; $i < $length; $i++) {
        $code .= self::get_random();
    }

    if(self::session_exists()) {
        $_SESSION[self::$captcha] = $code;
    }

    self::$width = $length * self::$character_width;
    
    return $code;
}

private static function get_width() {
    return self::$width;
}
private static function get_height() {
    return self::$height;
}

public static function image() {

    $length = 6;

    $code = self::generate_code($length);

    self::set_font();

    ob_start();
    
    $image = imagecreatetruecolor(self::get_width(),self::get_height());

    $white = imagecolorallocate($image, 255, 255, 255);

    imagefilledrectangle($image, 0, 0, self::get_width(), self::get_height(), $white);

    for($dot = 0; $dot < 2000; $dot++) {
        $r = rand(0,255);
        $g = rand(0,255);
        $b = rand(0,255);

        $dot_color = imagecolorallocate($image, $r, $g, $b);

        $x1 = rand(0, self::get_width());
        $y1 = rand(0, self::get_height());
        $x2 = $x1 + 1;
        $y2 = $y1 + 1;

        imageline($image, $x1, $y1, $x2, $y2, $dot_color);
    }

    for($start = - $length; $start < 0; $start++) {
        $color = imagecolorallocate($image, rand(0,177), rand(0,177), rand(0,177));

        $character = substr($code, $start, 1);

        $x = ($start+6) * self::$character_width;
        $y = rand(self::get_height() - 20, self::get_height() - 10);

        imagettftext($image, self::$font_size, 0, $x, $y, $color, self::$font, $character);
        
    }
    
    imagepng($image);
    imagedestroy($image);
    
    $source = ob_get_contents();
    ob_end_clean();

    unlink(self::$font);

    return "data:image/png;base64,".base64_encode($source);

 }
public static function  get_code() {
    if(self::session_exists()) {
        return $_SESSION[self::$captcha];
    }

    return rand();
   }
 }

index.php file

<?php
 session_start();

 require_once("captcha.php");

  if(isset($_POST['rCaptcha'])) {
   echo captcha::image();
  exit;
}
 else if(isset($_POST["code"])) {
  if($_POST["code"] == captcha::get_code()) {
    echo "Good";
  }
    else {
           echo "Bad";
    }

 echo "<br/>";
    }

    ?>
   <script>
   function refreshCaptcha(target) {

  var req = new XMLHttpRequest();

  req.open("POST", window.location, true);
  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");


   req.onreadystatechange = function() {
    if(req.readyState == 4 && req.status == 200) {
        target.src = req.responseText;
    }
   }

    req.send("rCaptcha=true");
  }
  </script>

  <form method="post" autocomplete="off">
   <fieldset>
    <legend>PHP Captcha</legend>
    
    <input type="text" name="code" placeholder="Captcha Code" /><br/>
    <img src="<?= captcha::image() ?>" onclick="refreshCaptcha(this)" 
    title="click to refresh" /><br/>
    <input type="submit" value="Check" /><br/>
    </fieldset>
   </form>
  • 1
    Post your code in your question. The link is fine as an addition, but all the code necessary to help you should be here in the question. – Stephen P Oct 28 '20 at 23:57
  • Blank page _usually_ implies an error happened. From the command line, try running `php -l yourfile.php` to check for possible syntax issues. – Paul T. Oct 29 '20 at 00:00
  • I did and get no errors @Paul T. – Salvatore Lucania Oct 29 '20 at 00:29
  • 1
    https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Barmar Oct 29 '20 at 00:34
  • Ok, the link that @barmar provided is another way, and there are also server logs that you could check too. Where the logs are located depends on what webserver that you have. – Paul T. Oct 29 '20 at 00:43
  • yeah found something it says this: Resource temporarily unavailable in C:\\xampp\\htdocs\\mysite\\captcha.php on line 123. Thats this line unlink(self::$font); – Salvatore Lucania Oct 29 '20 at 00:46
  • It seems the file is still in use when you try to 'unlink' it. I think it's reasonable to comment out this line at first. Since the file is in temp directory, I guess it will be removed one day by the system (not an expert here). – Xetolone Oct 29 '20 at 01:21
  • yes problem solved now it displaying image. Thank you very much for help. – Salvatore Lucania Oct 29 '20 at 01:27

1 Answers1

0

For a start add those two lines in the beginning of your index.php file (after <?php):

error_reporting(-1);
ini_set('display_errors', 'On');

Then you will see the errors produced by php. It will be much more easy to debug your code!

In this case a semicolon is missing in line 16 in your captcha.php file:

$AnonymousClippings ='there is inserted chars from font you can'
Xetolone
  • 357
  • 1
  • 12
  • my bad semicolon was there. when i copy the code i didnt copy semicolon – Salvatore Lucania Oct 29 '20 at 00:50
  • So, do you have any error message left? You sometimes have to check in the source code of the page with Ctrl + u to see them because they're hidden in the HTML. – Xetolone Oct 29 '20 at 00:57
  • yes i found it in server log and it says this : Resource temporarily unavailable in C:\\xampp\\htdocs\\mysite\\captcha.php on line 123 its the line with unlink(self::$font); – Salvatore Lucania Oct 29 '20 at 01:01