Tuesday, 26 March 2013

PHP creating and using captcha – Best and simple way


PHP creating and using captcha – Best and simple way

How to create simple but powerful captcha using php.How to use captcha to the form to avoid spams.I’ll describes how to create & use captcha using php.
This is the contact form.
<!-- Error display -->
<?php if(!empty($error)) echo '<div class="error">'.$error.'</div>'; ?>
 <?php if(!empty($success)) echo '<div class="success">'.$success.'</div>'; ?>
<!-- form -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <p>Name<input type="text" name="name" /> </p>
    <p>Email<input type="text" name="email" /> </p>
    <p>Message<textarea name="message"></textarea></p>
    <img src="captcha.php" id="captcha"/>
    <p> Are you human?<input type="text" name="captcha_code" id="captcha_code"/></p>
    <a href="#" onclick="document.getElementById('captcha').src='captcha.php?'+Math.random();
    document.getElementById('captcha_code').focus();"  id="change-image">Not readable? Change text.</a><br/><br/>
    <p><input type="submit" name="submit" value="Send" class="button" /></p>
</form>

This is the php code to handle the submit process.Add this code form submitted page.
Select Code
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
<?php
session_start();
if(isset($_POST['submit'])) {
    if(!empty($_POST['txtName']) && !empty($_POST['txtEmail']) && !empty($_POST['txtMessage']) && !empty($_POST['captcha_code'])) {
        if($_POST['captcha_code'] == $_SESSION['random_code']) {
            // send email
            $success = "Thank you for contacting me.";
        } else {
            $error = "Please verify that you typed in the correct code captcha.";
        }
    } else {
        $error = "Please fill out the entire form.";
    }
}
?>

[stextbox id="info"]Note: Every time captcha code is keeping as a session variable.Because in the form validation process, validate captcha is correct or not with comparing session captcha value and form submitted value.[/stextbox]
You already noted captcha.php file.It is generated captcha code by creating image using php. captcha.php file
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
<?php
    session_start();
    $string = '';
    for ($i = 0; $i < 5; $i++) {
        // this numbers refer to numbers of the ascii table (lower case)
        $string .= chr(rand(97, 122));
    }
    $_SESSION['random_code'] = $string;
    $fontDir = 'fonts/'; // font folder
    $image          = imagecreatetruecolor(170, 60);
    $textColor      = imagecolorallocate($image, 250, 40, 50); // red color, give as RGB color
    $imageBgColor   = imagecolorallocate($image, 255, 255, 255);
    imagefilledrectangle($image,0,0,399,99,$imageBgColor);
    imagettftext ($image, 30, 0, 10, 40, $textColor, $fontDir."BRUSHSCI.TTF", $_SESSION['random_code']);
    header("Content-type: image/png");
    imagepng($image);
?>
If you want to change font in tha captcha, keep your font file in the fonts folder. That’s only.

No comments:

Post a Comment