The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > PHP


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-11-2007, 10:34 PM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
How TO Make BASIC CAPTCHA in PHP

CAPTCHA is stand Completely Automated Public Turing test to tell Computers
and Humans Apart. Have you ever register for Yahoo email account and
Yahoo required you to type any letters that contains in image. It is call
CAPTCHA. This method is for prevent or reduce the attacker. In this tutorial,
I will explain you how to make a very basuc CAPTCHA.

Requirement:
- PHP 4+ (not sure if it is work in PHP3)
- GD library
__________________________________________________ _____

STEP 1: Generate String
The very first step is to generate the randomly 5 letters string. It is up to
you to determine how many letters string you want, but I choose 5 letters.

PHP Code:
    $string md5(microtime() * mktime());
    
$string substr($string05); 
I used microtime and mktime to generate a random number and encrypted
with md5 function. mn5 will generate 32 characters string and we substr
to cut 5 letters from the encrypted string.
__________________________________________________ _____

STEP 2: Generate Image
Now we got random string in our hand. Let make it into image. First I create
one background image (you can make it in photoshop or any program):


(back.jpg)

PHP Code:
    $im imagecreatefromjpeg('back.jpg'); 
imagecreatefromjpeg is function to create the image by loading from exist
jpeg image file. Now we got background in our image. Let print some
strings out.

PHP Code:
    $white_color imagecolorallocate($im255255255);
    
imagestring($im5257$string$white_color); 
imagecolorallocate took 4 arguments, the first one is the resource of image
and the last 3 arguments are RED, GREEN, BLUE which each arguments
is range from 0 to 255. Basically, if RED=255, GREEN=0, BLUE=0 mean it is
RED color. Another example: if RED=255, GREEN=255, BLUE=0 then the color
is Yellow. We use imagecolorallocate to create white colour. imagestring
is function to print the string out into the image. imagestring took 6
arguments. imagestring(image, fonts, x-possition, y-possition, string to
print, color of the string)
. There are 5 fonts are build in. x and y position
are string position in the image: here it is how x and y work:


PHP Code:
    for($i 0$i 5$i++) {
        
imageline($imrand(0100),rand(020), rand(50100), rand(50100), $white_color); 
    } 
Now we draw 5 lines to make our image is difficult for robot to read through
imageline took 6 arguments. imageline(image, x1, y1, x2, y2, colour).
x1 and y1 is starting position and x2 and y2 is ending position. Here
it is how it position work:


PHP Code:
    imagejpeg($im'verify.jpg'); 
    
imagedestroy($im); 
Now we save the image as 'verify.jpg' and free image from memory. Now we
got nice image generated.
__________________________________________________ _____

STEP 3: Save it into SESSION
PHP Code:
    session_start();
    
// our code in step 1 and step 2
    
$_SESSION['vcode'] = md5($string); 
Put session_start(); at the beginning of code and we use $_SESSION
to remember the string in our image. In this case I have encrypted the
string to make it more secure.
__________________________________________________ _____

STEP 4: Check For Correct Input
Now this the final step of how to CAPTCHA is to check if the users input
the right code we provided:

PHP Code:
<form method="POST" action="">
    <
img src="verify.jpg"><br>
    <
input type="text" name="textcode">
    <
input type="submit" value="Verify" name="submit">
</
form
First create the form with image and one textbox for users to input.
Here it is how we check if we got correct input or not:

PHP Code:
    if (isset($_POST['submit']) && 
        
md5($_POST['textcode']) == $_SESSION['vcode']) {
        die (
'Correct');
    } else {
        echo (
'Incorrect');
    } 
__________________________________________________ _____

That is all for my tutorial. It is very basic CAPTCHA application. If you want
source code? you can tell me to post it here.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 3 Users Say Thank You to siLenTz For This Useful Post:
HelloWorld (07-12-2007), Lee (07-12-2007), rpgfan3233 (07-12-2007)
  #2 (permalink)  
Old 07-12-2007, 11:12 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 417
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
Full source would be appreciated. I'll add to it when you post as I have a captcha setup that also uses sound on my own site. I believe it uses the same functions (which do work on PHP 5).

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 07-12-2007, 05:05 PM
ablaye
Posts: n/a
I believe there are already some free captcha scripts out there. No need to re-invent the wheel.
Anyway, it's always good to know how to do it. Thanks for the post.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-12-2007, 06:46 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by ablaye View Post
I believe there are already some free captcha scripts out there. No need to re-invent the wheel.
Anyway, it's always good to know how to do it. Thanks for the post.
I think it's important if he created something that's better from the one that's been inveted Well, it's just the matter of design and algorithm. There are many algorithm that does the same thing, but do they really do on the huge data? **That's what I'm learning right now, algorithm analysis** kind of boring but still have to go through it...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-12-2007, 07:14 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 417
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
There are also many applications. What has really been demonstrated is how to create a graphic and overlay any text you want (with extraneous lines, if desired.) Every code snippet will have its use to someone and the different presentation may allow someone to "finally get it."

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 07-12-2007, 07:14 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
If you want to code things then theres no point just getting other peoples scripts, its better to make it your self and understand how its built up ablaye.

Thanks for tutorial

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 07-12-2007, 08:26 PM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
Full source:

PHP Code:
<html>
<head>
    <title>CAPTCHA</title>
</head>
<body>
<?php
    session_start
();

    if (isset(
$_POST['submit']) && 
        
md5($_POST['textcode']) == $_SESSION['vcode']) {
        die (
'Correct');
    } else {
        echo (
'Incorrect');
    }

    
$string md5(microtime() * mktime());
    
$string substr($string05);

    
$im imagecreatefromjpeg('back.jpg');

    
$white_color imagecolorallocate($im255255255);

    
imagestring($im5257$string$white_color);

    for(
$i 0$i 5$i++) {
        
imageline($imrand(0100),rand(020), rand(50100), rand(50100), $white_color); 
    }

    
imagejpeg($im'verify.jpg'); 
    
imagedestroy($im);

    
$_SESSION['vcode'] = md5($string);
?>
<form method="POST" action="">
    <img src="verify.jpg"><br>
    <input type="text" name="textcode">
    <input type="submit" value="Verify" name="submit">
</form>
</body>
</html>

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-12-2007, 11:10 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
$white_color = imagecolorallocate($im, 255, 255, 255);
that line right is is pretty cool, is that mixing the colors?
I never knew that there's a language that done all of these already, but I think if they can break up the algorithm for imagecolorallocate(); then your CAPTCHA won't do much... I think we should create it our own version for that

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #9 (permalink)  
Old 07-12-2007, 11:32 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 417
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
I recently read an ACM article which talked about CAPTCHA and it indicated that at all tweakings of the graphic, an automated script (not named by them in the article) did better than humans in reading the graphic. Now, reCAPTCHA: Stop Spam, Read Books is a slightly different take using graphics known to cause OCR progs problems.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-13-2007, 12:01 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
I recently read an ACM article which talked about CAPTCHA and it indicated that at all tweakings of the graphic, an automated script (not named by them in the article) did better than humans in reading the graphic
I think this is kind of fake...? Is this even possible XD!!! The end of human era then..? lol...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:28 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50