<?php
session_start();
ob_start();

if (empty($_SESSION['rand_code'])) {
    $string = '';
    
    for ($i = 0; $i < 5; $i++) {
    	// this numbers refer to numbers of the ascii table (lower case)
    	$string .= chr(rand(97, 122));
    }
    
    $_SESSION['rand_code'] = $string;
    
    $_SESSION['captcha_time'] = time() + 600;
} 

if (isset($_GET['reset']) OR $_SESSION['captcha_time'] < time()) {
    $string = '';
    
    for ($i = 0; $i < 5; $i++) {
    	// this numbers refer to numbers of the ascii table (lower case)
    	$string .= chr(rand(97, 122));
    }
    
    $_SESSION['rand_code'] = $string;
    
    $_SESSION['captcha_time'] = time() + 600;    
} 
  
    $dir = 'fonts/';
    
    $image = imagecreatetruecolor(100, 60);
    $black = imagecolorallocate($image, 0, 0, 0);
    $color = imagecolorallocate($image, 200, 100, 90); // red
    $white = imagecolorallocate($image, 255, 255, 255);
    
    imagefilledrectangle($image,0,0,399,99,$white);
    imagettftext ($image, 20, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['rand_code']);
    
    header("Content-type: image/png");
    imagepng($image);

?>