[TUT] Captcha Image Tutorial (No Spam Registrations and emails!)

Junior Spellweaver
Joined
Jul 6, 2008
Messages
173
Reaction score
2
Well its easier than you think

Things Using
  • 1 Font File
  • 2 Images
  • 2 Php Files

Example
http://darkcoders.co.nr/cap/

So lets get the php files together.
index.php
PHP:
<?php
//Creates the session to make sure the image matches text, and creates the form
session_start();

if(isset($_POST['submit']) && !empty($_POST['code'])){
$ses = $_SESSION['captcha'];
if($ses == $_POST['code']){
echo "Code is correct!";
}else{
echo "Code is incorrect!";
}
}else{
echo "<img src='captcha.php'><br>";
echo "<form action='' method='post'>"
	."<input type='text' name='code'>"
	."<br><input type=image src=others/submit.bmp name='submit' value=Submit alt=Submit><a href=index.php><img src='Others/refresh.bmp' border=0></a><br>"
	."</form>";
}
?>

captcha.php
PHP:
<?php
//Creates The Image
session_start();
$im = imagecreatefromjpeg ("others/image.jpg");
$rand1 = rand(1,255);
$rand2 = rand(1,255);
$rand3 = rand(1,255);
$color = imagecolorallocate($im, $rand1, $rand2, $rand3);
$text = rand(1000, 9999);
$font = 'others/TACOBOX_.TTF';
$size = 20;
imagettftext($im, $size, 0, 5, 28, $color, $font, $text);
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im);
$_SESSION['captcha'] = $text;
?>

Download the attachment for everything together
 

Attachments

Why write a tutorial for something that you can find in less than 10 seconds on google? Especially when the tutorial you're writing isn't even as good the ones you can find on google.
 
You may find that 10 sec on google BUT

But i customized this one.
And people can write a source off this one.

Its just a cool little captcha this for forms
so F*** Off
 
Nice tutorial, quite helpful for beginners :).
I always used MySQL to store the IP and code...but this is just as good (or maybe better).

Anyways, the first line is redundant:
PHP:
$ses = $_SESSION['captcha'];
if($ses == $_POST['code']){
Also would I strongly recommend indention, like this:
PHP:
<?php
session_start(); //Creates the session to make sure the image matches text, and creates the form

if (isset($_POST['submit']) && $_POST['code'])
{
    if ($_SESSION['captcha'] == $_POST['code'])
    {
        echo 'Code is correct!';
    }
    else
    {
        echo 'Code is incorrect!';
    }
}
else
{
    echo '<img src="captcha.php" alt="Captcha"><br>';
    echo '<form action="index.php" method="post">
          <a href="index.php"><img src="Others/refresh.bmp"></a><br>
          <input type="text" name="code"><br>
          <input type="submit" name="submit" value="Submit">
          </form>';
}
?>
Also adding different angles to each letter, and size and colour would be even better :), because these images can be cracked quite easily these days. And using PNG instead of BMP can decrease bandwidth usage quite a bit :)
 
Ok thanks, ill look into angels if i can.

Well, do you think a more dotted font or a font with lines through would make it just as good?

I new a had to get some cc ;)
 
Best is to use a different font for each letter ;)
 
Back