Search
Left Quote    Wise men speak because they have something to say; Fools because they have to say something.
- Plato (427-347 B.C.)    
Right Quote
 
[login] | [Register]
 

CAPTCHA Image

by: Free2Code
 

Description:


To gain use of this code sample, each step below must be followed:

  1. First, you need to copy the code sample provided and save it as a php file.
  2. Next, create a background and a cover image or image. The background can be some kind of simple texture, not too dark though. Cover image can be lines. Play around with it to get the effect you want. In the code sample, replace the links to the correct paths to the images you created.
  3. On the form you would like to protect from spam prevention, you need to insert the script as an image. That code woud look something like this:

    <img src="http://www.yoursite.com/path/to/CAPTCHA.php">

  4. Next, create an html text box (within a form) for the user to enter the CaSe sEnSiTiVe code that appears from the image/script. That code would look something like this:

    Enter Code: <input type="text" id="verify" name="verify" value="">

  5. Then in the validation part of your form, you need to ensure that the text box is not blank, and then if the values entered in the text box are equal to the code generated. The code generated will be stored in $_SESSION['code']; you can change that to whatever you want. Here is an example code to do that:

    if(!$_POST['verify']) {
        die('Verify field was blank.');
            } else {
    if($_SESSION['code'] != $_POST['verify']) {
        die('You did not enter the code from the box correctly.');
    }


If you have covered all of the steps above, it should work fine for you. Play around with it and make it your own! Enjoy, -bs0d.


Code:
<?
session_start
( );
$_SESSION['code'] = "";

//generate code
$string substrcryptrand( ) ), -);

//unwanted characters
$junk = array('.' ',' '/' '' '`' ';' '[' ,  ']' '-''_''*''&''^''%''$''#''@''!''~''+''('')''|''{''}''<''>''?'':''"''=');

//strip unwanted              
$string str_replace$junk''$string );

//image background
$im imagecreatefrompng"http://www.yoursite.com/path/to/background_image.png" );
$orange imagecolorallocate$imrand(1,100), rand(1,100), rand(1,100) );

$half imagesx$im ) / 2;
$start rand10$half 1.5 );
$y imagesy$im ) / 2;
$pos $start;

for ( 
$i 0$i strlen$string ); $i++ ) {
    
imagestring$im100$pos$y$string[$i], $orange );
    
    
$pos += rand(11,15);
    
$y += 5-rand(0,10);
}
//image cover
$cov imagecreatefrompng"http://www.yoursite.com/path/to/cover_image.png" );
imagecopyresampled$im$cov0000300100300100 );

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

$_SESSION['code'] = $string;
session_write_close();
?> 


 


Comments:

  gravy
  Subject: "Doesnt work for me" Date: Jun 10 2007 at 8:32 pm    

And I dont know why.....
  WintersDance
  Subject: "Session varible not working?" Date: Jun 19 2007 at 6:54 am    

Following the script I noticed that even when I enter the code exactly as I see it in the picture it still tells me that "You did not enter the code from the box correctly.&quo t;

I'm thinking that the session['code '] varible is not being read while being compared to the input field?
You Must be signed in or a member to comment.


Code Stats

Code Stats

20,294 Views
2 Total Comments
3 Rating of 5 ( Votes)

Options

Code Options

· Login to Rate This Code
· Login to Post a Comment
· Read more by this author
Digg This Code! Del.icio.us: Bookmark This Code Reddit: Bookmark This Code! BlinkList: Blink This Code! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Code Spurl: Mark This Code!

Code Samples

Related    

· Database Connection
· Users Online
· Pagination
· Comments Script
· IdealMySQL Class for PHP 5.0





"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank