Author Topic: Creating a CAPTCHA with PHP  (Read 3355 times)

0 Members and 1 Guest are viewing this topic.

Offline admin

  • Administrator
  • *****
  • Posts: 43
  • Karma: +1/-0
  • Gender: Male
Creating a CAPTCHA with PHP
« on: May 25, 2008, 03:00:38 PM »
This tutorial will show you how to create a form with Captcha.
1.Sessions in PHP are started by using the session_start() function.  Like the setcookie( ) function, the session_start( ) function must come before any HTML, including blank lines, on the page.
Code: [Select]
session_start();
2.Imagecreatefrompng create a new image from file or URL.

Code: [Select]
$img = imagecreatefrompng('black.png');
3.The function imagecolorallocate creates a color using RGB (red,green,blue) format.

Code: [Select]
$white = imagecolorallocate($img, 255, 255, 255);
5.Displaying the random text on the captcha image

Code: [Select]
$numero = rand(100, 999);
4.Check if the security code and the session value are not blank and if the input text matches the stored text.

Code: [Select]
if(($_POST['check']) == $_SESSION['check']) {
echo 'Input O.K.';
}else{
echo 'Input Wrong';
        }

black.png


40 x 20 pixels

index.php


Code: [Select]
<form method="POST" action="cc.php">
<img src="captchas.php"> <br>
<input type="text" size="10" name="check"> 
<input type="submit" name="submit" value="submit">
</form>

captchas.php

Code: [Select]
<?php 
session_start
(); 
//imagecreatefrompng â€” create a new image 
//from file or URL
$img imagecreatefrompng('black.png'); 
//displaying the random text on the captcha image
$numero rand(100999); 
$_SESSION['check'] = ($numero); 
//The function imagecolorallocate creates a 
//color using RGB (red,green,blue) format.
$white imagecolorallocate($img255255255); 
imagestring($img1083$numero$white); 
header ("Content-type: image/png"); 
imagepng($img); 
?>

cc.php


Code: [Select]
<?php 
//Sessions in PHP are started by using the 
//session_start() function.  Like the setcookie( ) function, 
//the session_start( ) function must come before any HTML, 
//including blank lines, on the page.
session_start(); 
//Check if the security code and 
//the session value are not blank 
//and if the input text matches the stored text

if(($_POST['check']) == $_SESSION['check']) { 
echo 
'Input O.K.';
}else{ 
echo 
'Input Wrong';
        }
?>
« Last Edit: May 25, 2008, 08:00:10 PM by admin »

Offline alex

  • Global Moderator
  • *****
  • Posts: 35
  • Karma: +11/-0
Re: Creating a CAPTCHA with PHP
« Reply #1 on: March 20, 2009, 03:58:23 PM »
The imagecreate () function is used in PHP to create a new palette based image. It's two parameters are width and then height of the image to be created.
Code: [Select]
<?php 
session_start
(); 
//imagecreate -- Create a new palette based image
$img imagecreate(4020);
//displaying the random text on the captcha image
$black imagecolorallocate($img000); 
$numero rand(100999);
$number =&#160; $black . $numero;
$_SESSION['check'] = ($numero); 

$white imagecolorallocate($img25500); 
imagestring($img1083$numero$white); 
header ("Content-type: image/png"); 
imagepng($img); 
?>


« Last Edit: March 20, 2009, 04:04:16 PM by alex »

 

Related Topics

  Subject / Started by Replies Last post
1 Replies
8292 Views
Last post January 29, 2009, 10:29:27 PM
by vivandmars
0 Replies
4159 Views
Last post April 15, 2009, 06:39:55 AM
by admin
11 Replies
5284 Views
Last post December 08, 2009, 10:52:33 AM
by admin