Main Menu
Articles Home
Most Popular Articles
Top Authors
Submit Articles
Submission Guidelines
Link to Us
Bookmark
Contact Us



Partners
 
Home / Computers

Step By Step: Add Image Validation To Your Website Form

By:David Picella


Copyright 2006 David Picella



If you are a do-it-yourself programmer and you want to know how to add image validation to a form on your web site then this is the article for you. While there are many ways to do this, if you follow these steps, you will learn the easiest way to get the job done. Once you have mastered the example here, you will have all the concepts and experience you need to incorporate the final product into your form on your website.



We will need two files. The first file will create the validation image and incorporate the image in a validation form. Here is the step-by-step code for the first file: verify.php



---BEGINNING OF FILE: VERIFY.PHP---



< ?php



// Create an image where width=200 pixels and height=40 pixels



$val_img = imagecreate(200, 40);



// Allocate 2 colors to the image



$white = imagecolorallocate($val_img, 255, 255, 255);



$black = imagecolorallocate($val_img, 0, 0, 0);



// Create a seed to generate a random number



srand((double)microtime()*1000000);



// Run the random number seed through the MD5 function



$seed_string = md5(rand(0,9999));



// Chop the random string down to 5 characters



// This is the validation code we will use



$val_string = substr($seed_string, 17, 5);



// Set the background of the image to black



imagefill($val_img, 0, 0, $black);



// Print the validation code on the image in white



imagestring($val_img, 4, 96, 19, $val_string, $white);



// Write the image file to the current directory



imagejpeg($val_img, "verify.jpg");



imagedestroy($val_img);



? >



< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"



"http://www.w3.org/TR/html4/loose.dtd" >



< html >



< head >



< meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >



< title >Image Verification< /title >



< /head >



< body >



< img src="verify.jpg" height="40" width="200" alt="validation image" / >



< br / >< br / >Type in the validation code from the image. (case sensitive)



< form action="validateform.php" method="post" >



< input name="random" type="text" value="" >



< input type="submit" >



< !-- The validation code will be posted from a hidden form element -- >



< input name="validation" type="hidden" value="< ?php echo $val_string ? >" >



< /form >



< /body >



< /html >



---END OF FILE: VERIFY.PHP---



Now we will create a file that will perform the validation using the POST method:



---BEGINNING OF FILE: VALIDATEFORM.PHP---



< ?php



if ($_POST['validation'] == trim($_POST['random'])){



echo "You are validated";



}



else{



echo "Please go back and get validated.";



}



? >



---END OF FILE: VALIDATEFORM.PHP---



IMPORTANT NOTE: Because many free article websites don’t support HTML in the body of the article the files above won’t work unless you eliminate the space after < and before >. Just do a find and replace all.



That is really all there is to it. You probably noticed that I also went to the trouble of including the DOCTYPE and charset lines in this simple example. I did this as a little reminder to make sure that all of your pages on your website are W3C compliant. One of the biggest mistakes that many webmasters make is to neglect W3C compliance on their web pages. If your web pages are not W3C compliant you will get low search engine rankings. Even worse, if you are advertising, your ads may not be as relevant as they should be! In my other article, I explain W3C compliance and the most common mistakes.



Digg del.icio.us Blink Stumble Spurl Reddit Netscape Furl

Article keywords: form html, forms html, html, html image, image php, javascript form validation, method post, php, php script, validation form, verification form, spam, anti spam, filter spam

Article Source: http://www.articles2k.com

Original article and source code for download are available at picella.com --The author, David Picella, is a PhD Student at the University of Wisconsin Milwaukee. In addition to his technical articles on technology and Internet subjects, he has written other articles on health care topics in women's health and reproductive care.







Top Computers Articles
  • 1). The danger behind peer-to-peer file sharing  By : Migz Mon
    File sharing is very common nowadays. You can get a P2P (peer-to-peer) program for free, and soon start sharing stuff like music, videos and programs. We use these applications freely and most of us don’t know the risks involved when using them. Questionable reliability of files – Once we get our hands on a P2P program, we download everything we want like crazy.

  • 4). Is The Internet Censored?  By : J Schipper
    When most people think about Internet censorship, the usual suspects immediately come to mind : Communist China and repressive Islamic countries. Unfortunately, Westerners have an embarrassment all their own : Australia's net censorship law, established in 2000. Freedom House, a non-profit U.S. organization dedicated to spreading human rights and freedom, undertakes regular surveys on press freedom and censorship.

  • 5). Toshiba Libertto U100, smaller yet better than you might imagine!  By : john
    Have you ever wondered how small a laptop can be? Well, utilizing today’s technology Toshiba does a perfect job at demonstrating it; the Libertto U100 is one of the very few laptops that have such a rich feature set fitted altogether within such a small space. Of course, if you believe that a fingerprint authentication system is too much for such a.

  • 6). Does CCTV Really Reduce Crime?  By : Robert Michael
    In the UK, home of twenty percent of the world's CCTV cameras, there is mounting concern about its invasion and lack of regulation. "UK Public CCTV Surveillance Regulation Campaign." The thought-provoking questions they ask are good ones. They want to know, for instance, why, if television programming and licensing is so heavily regulated in the UK.

  • 8). Preparation Tips For Comptia A+ Certification  By :
    The Industry Standard CompTIA A Plus Certification exam has two parts; approximately 80 questions for each part. * Core Hardware: $153 - min. passing score 515 * Operating Systems Technology: $153 - min. passing score 505 (CompTIA corporate members can take the exams for $98, employers that require this test are usually corporate members and will inform you of this.

  • 9). Can Cell Phones Harm Your Health?  By : D Ruplinger
    It seems like everywhere a person goes there is at least one person in view with a cell phone to their ear whether it is on the road, in a store, in a parking lot, walking down the street, etc. Even in places where cell phone usage is banned such as concert halls or movie theaters there is the occasional offender, or more likely, at least a few people using the text messaging feature on their phone.


New Computers Articles
  • 5). If You Want Flexibility And Portability Consider A Laptop  By : Gregg Hall
    If you are one of those who works with a computer on a daily basis like me you should consider a laptop. I have actually owned three now and I think I would be lost without mine. I still use my desktop PC quite a bit at home but the laptop comes in extremely handy when I am away.

  • 7). Becoming A DVD Repair Expert Online  By : Maxine Schel
    Many conventional colleges and universities are now offering online DVD repair courses, which are the exact same as those taught on a traditional college campus, to distance learners. Online education is generally taught through the use of several different methods, including online satellite feeds, correspondence or online lessons. This technique permits the student to obtain a DVD repair degree without ever having to step inside of a classroom.

  • 8). Compatible Inkjet Cartridge  By : Isabel Rodrigues
    Once the printer ink runs dry it has to be replaced with another inkjet cartridge. There are many reputed companies like Canon, Epson, Dell, and Lexmark that provide the necessary cartridges to replace the empty cartridges. Replacing inkjet cartridge can add to a very big cost. It could be worse if you have to replace the empty cartridges frequently every month.

  • 9). Best Passwords  By : Richard Romando
    No sane person would ever like someone else reading her email. Or for that matter some other person using her password and breaking into a financial institution. You should, therefore, choose a strong, secure password in such a manner that would be a hard nut to crack for others and easy for you to remember. The more random and mixed-up you make it, the harder it is for others to crack.

  • 10). Home Printing Machines  By : Peter Vermeeren
    Home printing machines are available on the market today in many sizes, prices and capabilities. Many different forms of printing can be accomplished in the home. Whether you want to create unique tee-shirts using a home screen printing machine or you want to produce elegant laser printed letters, the equipment is readily available to you for home use.



 


© 2006 articles2k.com - Privacy Policy