2

I'm currently developing a captcha system for the site I'm currently developing for a school project.. here: http://rjtestsystem.atwebpages.com/

Here's the code, err, prototype I came up with so far:

 <?php
    if (isset($_GET['METHOD']) && $_GET['METHOD']=='captcha')
    {   session_start();
        $code=rand(1000,9999);
        $_SESSION["code"] = $code;
        session_write_close();
        $font = rand(1,4).".ttf";
        if ($font == "1.ttf")
            $font_size = 20;
        else if ($font == "2.ttf")
            $font_size = 25;
        else if ($font == "3.ttf")
            $font_size = 30;
        else if ($font == "4.ttf")
            $font_size = 35;
        $angle = 0;
        $width = 120;
        $height = 60;
        $bounding_box = imagettfbbox($font_size, $angle, $font, $code);
        $textwidth = abs($bounding_box[4] - $bounding_box[0]);
        $textheight = abs($bounding_box[5] - $bounding_box[1]);
        $x = ($width - $textwidth) / 2;
        $y = (($height*3/4) + $textheight) / 2;
        $image = imagecreatetruecolor($width, $height);
        $text_background = imagecolorallocate($image, 0, 0, 0);
        $color = imagecolorallocate($image, 230, 230, 230);
        imagefill($image, 0, 0, $text_background);
        imageline($image, 0, $y-rand(0,10), $width, $y-rand(0,10), $color);
        imageline($image, 0, $y, $width, $y, $color);
        imageline($image, 0, $y+rand(0,10), $width, $y-rand(0,20), $color);
        imageline($image, $x+rand(0,$width/2), 0, $x+rand($width/2,$width), $height, $color);
        imageline($image, $x+rand(0,$width/2), $height, $x+rand($width/2,$width), 0, $color);
        for($i=0;$i<400;$i++)
        {   imagesetpixel($image,rand()%$width,rand()%$height,$color);
        }
        imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $code);
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-type: image/png');
        imagepng($image);
        imagedestroy($image);
        exit();
    }
    ?>
    <img src = "<?php echo $_SERVER['PHP_SELF']; ?>?METHOD=captcha">

The code seems to be working fine, but when I try to move the files elsewhere, the system would give me a message that the file is being used by another program. It seems that the references to the TTF files are still there even after executing the code. I've tried to look for a way to stop this behavior but I can't seem to get my searches right. Well, the problem goes away once I restart the server, but it's a hassle when debugging. Is there a way or proper method to remove the references / file handle / etc to the TTF files or close this process as soon as it's finished? If possible, I'd like to keep using this code.

I have only a year of experience in Php so please bear with my rudimentary way of coding and asking questions. If anyone can lead me to the right discussions, please do. Thank you.

I'm using WAMP server version 2.4.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
RJBaytos
  • 71
  • 1
  • 8

0 Answers0