05-17-2009, 05:27 PM
Well I was making a site and had to create some sort of CAPTCHA,
Seems to work decently, It generates a very random string, that is 5 to 7 characters long
And uses a random color aswell as a somewhat random posistion, and throws 2 lines through the image then the captcha string is stored in $_SESSION['captcha']
PHP Code:
<?php
session_start();
mt_srand(time());
$my_img = imagecreate( 200, 40 );
$bg = imagecolorallocate($my_img, 0, 0, 0);
$blue = imagecolorallocate($my_img, 0, 0, 255);
$gray = imagecolorallocate($my_img, 169, 169, 169);
$wild = imagecolorallocate($my_img, mt_rand(1,255), mt_rand(1,255), mt_rand(1,255));
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 10, $gray );
imageline( $my_img, 35, 15, 122, 1, $blue );
$str = substr(md5(microtime()*mktime()),0,mt_rand(5,7));
$_SESSION['captcha'] = $str;
imagestring( $my_img, 4, mt_rand(20,30), mt_rand(15,25), $str, $wild );
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>Seems to work decently, It generates a very random string, that is 5 to 7 characters long
PHP Code:
$str = substr(md5(microtime()*mktime()),0,mt_rand(5,7));
And uses a random color aswell as a somewhat random posistion, and throws 2 lines through the image then the captcha string is stored in $_SESSION['captcha']

