alona Rudnitsky offers the following royalty-free article for you to publish
online or in print.
Feel free to use this article in your newsletter, website, ezine, blog, or
forum.
-----------
PUBLICATION GUIDELINES
- You have permission to publish this article for free providing the "About the
Author" box is included in its entirety.
- Do not post/reprint this article in any site or publication that contains
hate, violence, porn, warez, or supports illegal activity.
- Do not use this article in violation of the US CAN-SPAM Act. If sent by
email, this article must be delivered to opt-in subscribers only.
- If you publish this article in a format that supports linking, please ensure
that all URLs and email addresses are active links.
- Please send a copy of the publication, or an email indicating the URL to
[email protected]
- Article Marketer (www.ArticleMarketer.com) has distributed this article on
behalf of the author. Article Marketer does not own this article, please
respect the author's copyright and publication guidelines. If you do not agree
to these terms, please do not use this article.
-----------
Article Title: How to Create Captcha with PHP
Author: alona Rudnitsky
Category: Web Development, Web Design, E-Commerce
Word Count: 886
Keywords: freelance hire, programmers freelance, freelance website, web
freelance web development
Author's Email Address: [email protected]
Article Source: http://www.articlemarketer.com
------------------ ARTICLE START ------------------
When you fill up registration forms in the internet, you need to type some
crazy letters/numbers before submitting the form. These are called captcha
acronym for Completely Automated Public Turing test to tell computers and
Humans Apart. It is a simple computer generated test to differentiate humans
from any automated programs. The captcha images are usually simple and easy to
understand by humans. It uses colors, mirror images and smudge effect to make
the strings difficult to read by any automated programs and spambots.
Using a Captcha in the website will ensure greater security and protect your
site from unwanted malicious codes and spambots. The modern captcha use various
methods to create tough image scripts impossible to read by automated computer
applications. Using angled line in the segmentation or crowding the entire
group of strings are some modern methods used to create an effective captcha
images. The latest captchas are designed to be accessible even by blind people.
The captcha works simply by generating a random string with combination of text
and numbers and embedded in images. Then it is stored in the session or cookies
of the web pages. The stored captcha is checked when the form is filled by the
users. The flow chart for doing this process is to create a random text,
writing text on the image, store the image in a session/cookie, show the image
while filling the form, receive and validate the user input and if it matches
perform the next action-usually form submit function.
How to create a random text in PHP
PHP is an open source scripting language used to develop websites. Using PHP,
we can easily generate highly secure captcha images in the web pages. To create
a random text the following functions of PHP is used. Microtime() and mktime().
This function is used to generate a number. After creating the number it is
encrypted using md5() function. This will generate a 32 character long
encrypted string. It will be very long for the webpage. Normally 5 to 10
character is used in the web pages. We can reduce the number of character in
PHP using substr() function.
The script:
?php
//Session start to store the code.
session_start();
//md5 function to encrypt the random string
$md5 = md5(microtime() * mktime());
/*
The substr function is used to trim the encrypted string
*/
$string = substr($md5,0,5);
?
Now the encrypted captcha is created with 5 characters. The session start
function in the beginning is used to store the captcha.
How to write the text on the images
After creating the text, it has to be embedded with the images to make it
difficult for the spambots to read. You can use GD an open source code library
for creating dynamic images in PHP. In the below script we are using some
existing image for the background. The image.pngfile is used in this script. We
are setting the colors and codes and allocate to the variables to the selected
image. Once it is allocated, we are also adding some more lines in the images
to make it difficult for the spambots to understand.
The script:
?php
/*
We are now placing some background image.
*/
$captcha = imagecreatefrompng("./image.png");
/*
Set the color, and lines using color codes.
*/
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);
/*
Adding some lines to make it harder for the spambots
*/
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
?
After the image is ready, we need to write the text on the image. This can be
done using the imagestring function. It writes the encrypted text on the image
just created by you.
The script for this action is;
?php
/*
To write the text to the image
*/
imagestring($captcha, 5, 20, 10, $string, $black);
How to store in the session:
After the captcha is completely ready for the execution, it is to be stored in
the session or cookies for the user to access. In the below script we are
explaining how to store the captcha in a session. We can use $SESSION function
to encrypt the store the key inside a session. The image output will display
the user what is the type of image is displayed in the screen.
/*
Store in a session
*/
$_SESSION['key'] = md5($string);
/*
Image output
*/
header("Content-type: image/png");
imagepng($captcha);
?
How to validate user input and result
The user now sees the captcha with images in his web page. Once he types the
text, it needs to be validated against the original random code generated by
the system. Use the following code to check the user input;
?php
session_start();
//Compare encrypt and user input
if(md5($_POST['code']) != $_SESSION['key'])
{
die("You have entered the wrong text Please enter the correct text");
}else{
echo 'You have entered the correct text';
}
?
This is a basic level captcha programming in PHP. You can try different options
based on this document to create complex captcha images for your web pages.
Some of the options such as using TTF font, random lines, text rotation and
words instead of string can be used to create an effective captcha images for
your website.
Have a freelance job? Want a freelancer job? Visit
Web Freelance web development at http://expertnerds.com
------------------ ARTICLE END ------------------
[Non-text portions of this message have been removed]