A Powerful and Simple Captcha in PHP

Post all your tuts or request for tuts here.
Post Reply
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

A Powerful and Simple Captcha in PHP

Post by Ark »

Hey guys, here's a simple way of making a Captcha in php to prevent bots from entering your page to spam and make your life miserable. :)


First off let's make a new page called myowncaptcha.php

We'll need some random words so let's start making them.

<?php

$vocals= array('a','e','i','o','u','y'); // Added "y" for more variety
$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');


$threechar = $vocals[rand(0,5)] . $letters[rand(0,25)] . $vocals[rand(0,5)];
$twochar = $vocals[rand(0,5)] . $letters[rand(0,25)];
$twochar2 = $vocals[rand(0,5)] . $letters[rand(0,25)];
$fourchar = $vocals[rand(0,5)] . $letters[rand(0,25)] . $vocals[rand(0,5)] . $letters[rand(0,25)];

You can make howmany characters you want.

$fourwords = array("$threechar", "$twochar", "$twochar2", "$fourchar");

$chosenword = $fourwords[rand(0,3)]; // The word they will write to access.

Now we'll write " write the 'numbered' word to access" but remember it has to be exactly as the chosenword variable.

echo "Write the ";

if ($chosenword == $fourwords[0])
{ echo "first "; }

elseif ($chosenword == $fourwords[1])
{ echo "second "; }

elseif ($chosenword == $fourwords[2])
{ echo "third "; }

elseif ($chosenword == $fourwords[3])
{ echo "fourth "; }

echo "word to access <br />";

foreach($fourwords as $value)
{
echo '"' . $value . '", '; // Dont confuse the double quotes, they're for the string.
}

Now let's write the form.

<form method="get" action="checkcaptcha.php">
<input name="captcha" size="8" type="text" />
<input type="submit" value="submit" />
<input name="chosenword" type="text" value="<?php echo $chosenword;?>" style="visibility: hidden;" /><?php //remember that this input still makes some space.?>
</form>

(the input "chosenword" was made to pass the variable through the url, ther're other ways to pass variables and maybe with more efficiency to another php page but this is the way I'd on this tutorial.)

Now lets make the checkcaptcha.php (it could be the reguser.php page instead, or any other page you want your captcha to work.)


<?php
if($_GET['captcha'] != $_GET['chosenword'])
{
echo "<h2>WRONG!!</h2> Write the correct word. ";
echo "<a href='myowncaptcha.php'>Go back!";
}
else
{
echo "LETS ROCK!"; /// put whatever you like. Probably you will use this to access the reguser.php
}

// GOOD LUCK!!
?>

so it will look something like this: Image

Here are the sources of this lil tutorial:
myowncaptcha.php

Code: Select all

<html>
<head>
</head>

<?php

//... MyOwnCaptcha made by ARK
//    For indie-resource.com



$vocals= array('a','e','i','o','u','y'); // Added "y" for more variety
$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');


$threechar = $vocals[rand(0,5)] . $letters[rand(0,25)] . $vocals[rand(0,5)];
$twochar = $vocals[rand(0,5)] . $letters[rand(0,25)];
$twochar2 = $vocals[rand(0,5)] . $letters[rand(0,25)];
$fourchar = $vocals[rand(0,5)] . $letters[rand(0,25)] . $vocals[rand(0,5)] . $letters[rand(0,25)];

$fourwords = array("$threechar", "$twochar", "$twochar2", "$fourchar");
$chosenword = $fourwords[rand(0,3)];  // The word they will write to access.

echo "Write the ";

if ($chosenword == $fourwords[0])
{ echo "first "; }

elseif ($chosenword == $fourwords[1])
{ echo "second "; }

elseif ($chosenword == $fourwords[2])
{ echo "third "; }

elseif ($chosenword == $fourwords[3])
{ echo "fourth "; }

echo "word to access <br />";

foreach($fourwords as $value)
{
echo '"' . $value . '", ';  // Dont confuse the double quotes, they're for the string.
}
?>
<body>
<form method="get" action="checkcaptcha.php">
<input name="captcha" size="8" type="text" />
<input type="submit" value="submit" />
<input name="chosenword" type="text" value="<?php echo $chosenword;?>" style="visibility: hidden;" /><?php //remember that this input still makes some space.?>
</form>


</body>
</html>
checkcaptcha.php

Code: Select all

<?php
if($_GET['captcha'] != $_GET['chosenword'])
{
	echo "<h2>WRONG!!</h2> Write the correct word. ";
	echo "<a href='myowncaptcha.php'>Go back!</a>";
}
else
{
	echo "LETS ROCK!"; /// put whatever you like. Probably you will use this to access the reguser.php
}

// GOOD LUCK!!
?>
So as far as bots can't read and analyze :mrgreen: you can be sure that this captcha will keep you away from bots.
Good luck!
indie-resource.com Rocks!
Orgullo Catracho
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: A Powerful and Simple Captcha in PHP

Post by SpiritWebb »

Thanks so much for this! Will be implementing this into our site later!! Great tutorial!!
Image

Image
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: A Powerful and Simple Captcha in PHP

Post by Jackolantern »

Very nice! I like it :) Some of the other techniques I have seen rely on asking questions, like "What is the 3rd rock from the sun?". The problem with systems like these is that, sadly, some people won't understand or won't know the answer so they won't be able to register. But this one pretty much guarantees anyone with a first-semester ability in English can get through :)
The indelible lord of tl;dr
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: A Powerful and Simple Captcha in PHP

Post by Ark »

It's a pleasure! and i'm glad this will be helpful to you spiritwebb!

Yes jackolantern you're right, for some people you just need to be so obvious! I'm glad you like it!
Orgullo Catracho
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: A Powerful and Simple Captcha in PHP

Post by Nexus »

Very nice Ark! I to will implement this into my registration of HP Destiny! :)
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: A Powerful and Simple Captcha in PHP

Post by Ark »

That's great man :!: !
Orgullo Catracho
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: A Powerful and Simple Captcha in PHP

Post by Xaleph »

Hehe i`ve seen this before, it works pretty good. It`s a humanized form of Captcha, much better then typing those f&*ing letters and digits ( where captialised letters count too.. ) to make "sure" you are human. It had it`s best time.
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: A Powerful and Simple Captcha in PHP

Post by Ark »

Yeah, i copied this style in http://www.switchonthecode.com, If you want to comment in a tutorial right below you have to use this method.
Orgullo Catracho
Post Reply

Return to “Tutorials”