View Single Post
  #1 (permalink)  
Old 04-29-2008, 09:20 AM
Jaan's Avatar   
Jaan Jaan is offline
Mod
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 935
Last Blog:
AdStar Ad Control Pa...
Rep Power: 17
Jaan is just really niceJaan is just really niceJaan is just really niceJaan is just really nice
Send a message via MSN to Jaan
Default Creation of captcha / Part II /Secure forms

Okay here's our code of this captcha image:

PHP Code:
<?php
header
('Content-type: image/jpg');
$im imagecreatefromjpeg ("image.jpg");
$rand1 rand(1,255);
$rand2 rand(1,255);
$rand3 rand(1,255);
$color imagecolorallocate($im$rand1$rand2$rand3);
$text rand(10009999);
$font 'text-font.otf';
$size 30;
imagettftext($im$size07045$color$font$text);
imagejpeg($im);
imagedestroy($im);
?>
But if we want to use this image on our sites we must add:

PHP Code:
$_SESSION['captcha'] = $text
on the bottom of our captcha file. So.. now let's make our form secure.

First of all we must start the session

PHP Code:
<?php
session_start
();
session_start(); - Starts the session

Now I'm using a form for checking that is the security code correct

PHP Code:
if(isset($_POST['submit'])){
$ses $_SESSION['captcha'];
if(
$ses == $_POST['code']){
echo 
"Code is correct!";
}else{
echo 
"Code is incorrect!";

if(isset($_POST['submit']) && !empty($_POST['code'])){ - If someone has clicked the submit button let's continue with our script
$ses = $_SESSION['captcha']; - Let's get the captcha code from the session
if($ses == $_POST['code']){ - If captcha's code is same as the code you posted
echo "Code is correct!"; - let's say that the code was correct
}else{ - but if they are not same
echo "Code is incorrect!"; - let's say that the code is not correct
} - This ends the if and else statement

PHP Code:
}else{
echo 
"<img src='captcha.php'><br>";
echo 
"<form action='' method='post'>"
    
."<input type='text' name='code'>"
    
."<input type='submit' name='submit' value='Enter'>"
    
."</form>";

}else{ - If anyone hasnt clicked the submit button let's display our form
echo "<img src='captcha.php'><br>"; - Shows our captcha image
echo "<form action='' method='post'>"
."<input type='text' name='code'>"
."<input type='submit' name='submit' value='Enter'>"
."</form>";
- Our form
} - This ends the if and else statement



And we are done.. that was so simple
You can customize this script for your forms

Here's the full script:

captcha.php
PHP Code:
<?php
session_start
();
$im imagecreatefromjpeg ("image.jpg");
$rand1 rand(1,255);
$rand2 rand(1,255);
$rand3 rand(1,255);
$color imagecolorallocate($im$rand1$rand2$rand3);
$text rand(10009999);
$font 'text-font.otf';
$size 30;
imagettftext($im$size07045$color$font$text);
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im);
$_SESSION['captcha'] = $text;
?>
index.php
PHP Code:
<?php
session_start
();

if(isset(
$_POST['submit']) && !empty($_POST['code'])){
$ses $_SESSION['captcha'];
if(
$ses == $_POST['code']){
echo 
"Code is correct!";
}else{
echo 
"Code is incorrect!";
}
}else{
echo 
"<img src='captcha.php'><br>";
echo 
"<form action='' method='post'>"
    
."<input type='text' name='code'>"
    
."<input type='submit' name='submit' value='Enter'>"
    
."</form>";
}
?>
I attached the files also
Enjoy!
Attached Files To view attachments in this forum your post count must be 1 or greater. You currently have 0 posts.
__________________


Cheap & Professional Web Design | Need help? Send a PM

Last edited by Jordan; 05-07-2008 at 01:00 PM.
Reply With Quote

Sponsored Links