Creating a Basic Captcha script

Posts 1–6 of 6 · Page 1 of 1
Creating a Basic Captcha script
Nowadays sites offer Captcha systems where you just have to include a javascript file, but ill teach you how to make a basic captcha.

This is what we are making:

Refresh and watch the captcha change.

In PHP make sure you have these brackets between your code
[PHP]<?php
/r code here
?>
[/PHP]
First step is to create our variables. So lets call our Letter length, and then give the characters to use as letters (the alphabets).

[PHP]
$length = 3;
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ';
[/PHP]
Change the Length to how many letters you want to show. I chose 3.

Now we need to count the amount of chars we have and add an empty var for now. For that we will use
strlen()
[PHP
$charslength = strlen($chars);
$randomstring = '';
[/PHP]
Here $charslength is the Chars length, which in this case is 52.

Next is a loop, ill try to explain the loop to you.
[PHP]
for ($i = 0; $i < $length; $i++) $randomstring .= substr($chars, rand(0, $charslength - 1), 1);[/PHP]
The loop is telling us that substr() is getting a character from the $char variable using a random number between 0 and Chars Length, which is 52.

Now by now $randomstring will now equal 3 random letters, but we aren't dont yet. Next step is mixing the letters with 3 digits.

[PHP]$number = rand(100, 999);
$captcha = $number . $randomstring;
$captcha = str_shuffle($captcha);
[/PHP]
This is telling us that $number will be a random number between 100-999 (3 digits), if you want more add a 0 to the left and a 9 to the right one.
Next we have $captcha, Captcha equals the numbers with the letters.
So Captcha would look something like 209FoW.
So what we are doing now we have $Captcha equal the previous $captcha but shuffled, so what would of been 209FoW, is now W092oF.

SO congrats, now just put
[PHP]echo $captcha;[/PHP]
and you get a random captcha each time. But wait, its in text form, that's not useful, seeing as any bot can copy that code, but what they cant copy is a image. So here is how to make it an image like above.

Our code should look something like this right now
[PHP]<?php
$length = 3;
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ';
$charslength = strlen($chars);
$randomstring = '';
for ($i = 0; $i < $length; $i++) $randomstring .= substr($chars, rand(0, $charslength - 1), 1);
$number = rand(100, 999);
$captcha = $number . $randomstring;
$captcha = str_shuffle($captcha);
?>[/PHP]

Lets add on to it, just enter abit, making some space between both codes.

[PHP]$handle = ImageCreate (90, 24) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 255, 0, 0);
$txt_color = ImageColorAllocate ($handle, 0, 0, 0);[/PHP]
Here we are making $handle creation a image that is 90 by 24.
bg_Color is the color of the background in RBG format, knowing that we know
255,0,0 = Red.
txt_color is showing to have 0 color for RBG, making it black.

Now we have our colors set and the handle set lets add the text then create the image.
[PHP]ImageString ($handle, 8, 8, 5, "$captcha", $txt_color);
header ("Content-type: image/png");
ImagePng ($handle);
[/PHP]

ImageString is telling us, we will use handle as our base, with 8 font. The next 2 numbers is the location, which is 8 Y axis, and 5 X axis. That is equivalent to if you go to MS Paint, put a selection just over a image, and see those 2 numbers below change. its x, y.

next we are using a header, to tell the PHP file to act as a PNG file.
Last code is to create the file using the ImagePNG function.

Now save the script, upload, and go to it.
Mine is
which is
Code:
http://ugleh.com/captcha.php
Full Code:
[PHP]<?php
$length = 3;
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ';
$charslength = strlen($chars);
$randomstring = '';
for ($i = 0; $i < $length; $i++) $randomstring .= substr($chars, rand(0, $charslength - 1), 1);
$number = rand(100, 999);
$captcha = $number . $randomstring;
$captcha = str_shuffle($captcha);


$handle = ImageCreate (90, 24) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 255, 0, 0);
$txt_color = ImageColorAllocate ($handle, 0, 0, 0);
ImageString ($handle, 8, 8, 5, "$captcha", $txt_color);
header ("Content-type: image/png");
ImagePng ($handle);
?>[/PHP]


Ill make another tutorial teaching how to put it to use.
Very nice. Perhaps I should put together a list of tutorials here.
I agree. I might compile one for you if i get bored.
Woah that's awesome but waay to much code for me xD
You can just change the color and stuff to make it hard to read right. and is this a way where spamer bots cant get in.
This can easier.

FULL CODE:
[php]<?php
$length = 3;
$chars = array_merge(range(a,z),range(A,Z),range(0,9));
$captcha = array_rand($randomstring, 2);

$handle = ImageCreate (90, 24) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 255, 0, 0);
$txt_color = ImageColorAllocate ($handle, 0, 0, 0);
ImageString ($handle, 8, 8, 5, $captcha, $txt_color);
header ("Content-type: image/png");
ImagePng ($handle);
?>[/php]
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us