WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 4 of 5 (graded 3 times)
Viewed : 6268
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

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

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();
?>



CAPTCHA[Image verification]
Categories : PHP, Security, GD image library, Graphics, Sessions
Generate image with random number (CAPTCHA)
Categories : PHP, GD image library, Graphics, Security
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
Advanced Image WaterMarker
Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented
Function to generate readable/remeberable random password
Categories : PHP, Security, Security
Simple PHP Bar Graph using GD library
Categories : PHP, GD image library, Graphics, Arrays
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
Line graphics generation library written in PHP + GD library (spanish comments)
Categories : PHP, Graphics, GD image library
a simple pie-chart in php3 (with gd)
Categories : PHP, Graphics, GD image library, Charts and Graphs
EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly.
Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library
imageMarker v 3.00 with new advanced features
Categories : PHP, PHP Classes, Graphics, GD image library
Dynamic Image Authentication System in Signup Pages (CAPTCHA)
Categories : PHP, Security, GD image library
Securing Web Forms with Simple PHP-CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart)
Categories : PHP, Security, GD image library, Sessions
Display a bar chart based on random values.
Categories : Graphics, PHP, GD image library, Charts and Graphs