WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A captcha image allows you to prevent spam posting when users reload the page and stop bots from submitting forms automatically. This version allows you to use your own fonts (.ttf) to show the text.
Categories : PHP, Security, Graphics, GD image library Click here to Update Your Picture
Wilson Aquino
Date : Dec 17th 2007
Grade : 5 of 5 (graded 1 times)
Viewed : 1888
File : 4741.rar
Images : No Images for this code example.
Search : More code by Wilson Aquino
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

index.php
<?php
session_start
();
///->
if(isset($_POST['submit'])){
   
$captcha = $_POST['captcha'];
   
$captcha = strtoupper($captcha); // force to upper cuz captcha characters are uppers and it have to be too, else, the user will have to capitalize it by himself
   
if(md5($captcha) == md5($_SESSION['CaptchaCode'])){
       
$_SESSION['CaptchaCode'] = NULL; // destroy previous captcha code, so, if the user reload, he'll get the error
       
echo 'Congratulations, Captcha code Passed';
    }else{
        echo
'Error, check the verification code please';
    }
}
?>
<?=$_SESSION
['CaptchaCode'];?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
    Verification Image:
    <input type="text" name="captcha" />
    <img src="captcha.php" alt="0" />
    <input type="submit" name="submit" />
</form>



captcha.php
<?
session_start
();
///->
class Captcha{
    var
$Image, $CNum, $Width, $Height, $Spacer, $Fonts, $Code, $TTF;
    var
$FDir = 'fonts/'; // Directory for fonts
   
var $Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // Allowed Characters
   
var $captcha_bg = 'captcha_bg.jpg'; // Captcha Background Image
   
var $AngleMin = -20; // min grades letter will be rotated
   
var $AngleMax = 20; // max grades letter will be rotated
    ///->
   
function Captcha($w = 180, $h = 50, $cn = 7){
       
$this->Image = imagecreatefromjpeg($this->captcha_bg);
       
$this->Width = $w;
       
$this->Height = $h;
       
$this->CNum = $cn;
       
$this->Fonts = array('Adler.ttf', 'Adonais.ttf', 'Alba.ttf', 'Baveuse.ttf', 'Heck.ttf');
       
//->
       
$this->Get_Space();
       
$this->GenerateCode();
    }
   
///->
   
function Get_Space(){ $this->Spacer = (int)($this->Width/$this->CNum); }
   
///->
   
function GenerateCode(){
       
mt_srand(microtime() * 1000000);
       
$this->Code = '';
       
//->
       
for($i = 0; $i < $this->CNum; $i++){
           
$key = mt_rand(0, strlen($this->Chars)-1);
           
$this->Code = $this->Code . $this->Chars{$key};
        }
        for(
$i = 0; $i < $this->CNum; $i++){
           
$key_1 = mt_rand(0, strlen($this->Code)-1);
           
$key_2 = mt_rand(0, strlen($this->Code)-1);
           
$tmp = $this->Code{$key_1};
           
$this->Code{$key_1} = $this->Code{$key_2};
           
$this->Code{$key_2} = $tmp;
        }
       
//->
       
$this->Code = htmlentities($this->Code, ENT_QUOTES);
    }
   
///->
   
function drawLines(){
        for(
$i = 0; $i < $this->LNum; $i++){
           
$Colour = rand(100, 250);
           
$colour = imagecolorallocate($this->Image, $Colour, $Colour, $Colour);
           
//->
           
$W = $this->Width; $H = $this->Height;
           
imageline($this->Image, rand(0, $this->Width), rand(0, $this->Height), rand(0, $this->Width), rand(0, $this->Height), $colour);
        }
    }
    function
drawCode(){
        for(
$i = 0; $i < $this->CNum; $i++){
           
$r1 = rand(90, 120); $r2 = rand(100, 130); $r3 = rand(110, 140);
           
$Letter = substr($this->Code, $i, 1);
           
$fcolor = imagecolorallocate($this->Image, $r1, $r2, $r3);
           
$font = $this->FDir.$this->Fonts[rand(0, count($this->Fonts)-1)];
           
$fsize = rand(24, 36);
            if(
$i > 0 && $i < $this->CNum-1){ $Angle = rand($this->AngleMin, $this->AngleMax); }else{ $Angle = 0; }
           
//->
           
$TB = imagettfbbox($fsize, $Angle, $font, $Letter);
           
$X = $this->Spacer / 5 + $i * $this->Spacer;
           
$iCharHeight = $TB[2] - $TB[5];
           
$Y = $this->Height / 2 + $iCharHeight / 4;
           
imagettftext($this->Image, $fsize, $Angle, $X, $Y, $fcolor, $font, $Letter);
           
        }
    }
    function
Draw(){
       
$this->drawCode();
       
//->
       
$_SESSION['CaptchaCode'] = $this->Code;
       
//->
       
header("Expires: 0");
        if(
function_exists('imagegif')){
           
header("Content-type: image/gif");
           
header('Cache-control: no-cache, no-store');
           
imagegif($this->Image);
        }elseif(
function_exists('imagejpeg')){
           
header("Content-type: image/jpeg");
           
header('Cache-control: no-cache, no-store');
           
imagejpeg($this->Image, null, 100);
        }else{
           
header("Content-type: image/png");
           
header('Cache-control: no-cache, no-store');
           
imagepng($this->Image);
        }
       
imagedestroy($this->Image);
        exit;
    }
}
$captcha = new Captcha;
$captcha->Draw();
?>



A damaged image generator (class) for validating text. CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart
Categories : PHP, PHP Classes, Security, GD image library, Security
CAPTCHA[Image verification]
Categories : PHP, Security, GD image library, Graphics, Sessions
Generate image with random number (CAPTCHA)
Categories : PHP, GD image library, Graphics, Security
Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality.
Categories : PHP, GD image library, Graphics
PHP Email image generator - hide your email from bots - using the GD Library
Categories : PHP, Graphics, GD image library, Beginner Guides
Display a bar chart based on random values.
Categories : Graphics, PHP, GD image library, Charts and Graphs
Advanced Image WaterMarker
Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented
Line graphics generation library written in PHP + GD library (spanish comments)
Categories : PHP, Graphics, GD image library
Creates a CAPTCHA image in PHP, which displays 5 numbers stored in a session.
Categories : PHP, GD image library, Form Processing, Security
Simple PHP Bar Graph using GD library
Categories : PHP, GD image library, Graphics, Arrays
Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs
Dynamic Image Authentication System in Signup Pages (CAPTCHA)
Categories : PHP, Security, GD image library
imageMarker v 3.00 with new advanced features
Categories : PHP, PHP Classes, Graphics, GD image library
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
PHP Image Compression using GD library
Categories : PHP, Compression, GD image library, Graphics