PHP Generated code picture program

Source: Internet
Author: User
The code is as follows Copy Code

<?php
Session_Start ();

if (Isset ($_post[' submit ')) {
if ($_session[' security_code '] = = $_post[' Security_code '] &&!empty ($_session[' Security_code '))) {
Insert your code for processing the "form" here, e.g emailing the submission, and entering it into a database.
Echo ' Thank you. Your message said "'. $_post[' message '. '";
unset ($_session[' Security_code '));
} else {
Insert your code for showing a error message
Echo ' Sorry, you have provided a invalid security code ';
}
} else {
?>

<form action= "form.php" method= "POST" >
<label for= "Name" >name: </label><input type= "text" name= "name" id= "name"/><br/>
<label for= "Email" >email: </label><input type= "text" name= "email" id= "email"/><br/>
<label for= "Message" >message: </label><textarea rows= "5" cols= "a" name= "message" id= "message" > </textarea><br/>
<br/>
<label for= "Security_code" >security Code: </label><input id= "Security_code" name= "Security_code" type = "Text"/><br/>
<input type= "Submit" name= "submit" value= "Submit"/>
</form>

<?php
}
?>

Validation Program captchasecurityimages.php

The code is as follows Copy Code

<?php
Session_Start ();

/*
* File:CaptchaSecurityImages.php
* Author:simon Jarvis
* copyright:2006 Simon Jarvis
* date:03/08/06
* updated:07/02/07
* requirements:php 4/5 with GD and FreeType libraries
* link:http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This are free software; You can redistribute it and/or
* Modify it under the terms of the GNU general public License
* As published by the free Software Foundation; Either version 2
* of the License, or (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU general public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

Class Captchasecurityimages {

var $font = ' Monofont.ttf ';

function Generatecode ($characters) {
/* List all possible characters, similar looking characters and vowels have been * *
$possible = ' 23456789bcdfghjkmnpqrstvwxyz ';
$code = ';
$i = 0;
while ($i < $characters) {
$code. = substr ($possible, Mt_rand (0, strlen ($possible)-1), 1);
$i + +;
}
return $code;
}

function captchasecurityimages ($width = ' a ', $height = ', ' $characters = ' 6 ') {
$code = $this->generatecode ($characters);
/* Font size would be 75% of the image height * *
$font _size = $height * 0.75;
$image = @imagecreate ($width, $height) or Die (' cannot initialize new GD image stream ');
/* Set the colours * *
$background _color = imagecolorallocate ($image, 255, 255, 255);
$text _color = Imagecolorallocate ($image, 20, 40, 100);
$noise _color = imagecolorallocate ($image, 100, 120, 180);
/* generate random dots in background * *
For ($i =0 $i < ($width * $height)/3; $i + +) {
Imagefilledellipse ($image, Mt_rand (0, $width), Mt_rand (0, $height), 1, 1, $noise _color);
}
/* Generate random lines in background * *
For ($i =0 $i < ($width * $height)/150 $i + +) {
Imageline ($image, Mt_rand (0, $width), Mt_rand (0, $height), Mt_rand (0, $width), Mt_rand (0, $height), $noise _color);
}
/* Create textbox and add text * *
$textbox = Imagettfbbox ($font _size, 0, $this->font, $code) or Die (' Error in Imagettfbbox function ');
$x = ($width-$textbox [4])/2;
$y = ($height-$textbox [5])/2;
Imagettftext ($image, $font _size, 0, $x, $y, $text _color, $this->font, $code) or Die (' Error in Imagettftext function ');
/* Output CAPTCHA image to Browser * *
Header (' Content-type:image/jpeg ');
Imagejpeg ($image);
Imagedestroy ($image);
$_session[' security_code ' = $code;
}

}

$width = Isset ($_get[' width ')? $_get[' width ': ' 120 ';
$height = isset ($_get[' height '])? $_get[' height ']: ' 40 ';
$characters = Isset ($_get[' characters ']) && $_get[' characters '] > 1? $_get[' characters ']: ' 6 ';

$captcha = new Captchasecurityimages ($width, $height, $characters);

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.