Php verification code Image project_php tutorial

Source: Internet
Author: User
Php generates a verification code Image program. A php verification code generated by a foreign website for download. this is a practical example. you can save it as php if you need it. The code is as follows: copy a php verification code Image code downloaded from a foreign website. this is a practical example. if you need to save it as php, you can use it.
The code is as follows:

Session_start ();

If (isset ($ _ POST ['submit ']) {
If ($ _ SESSION ['security _ Code'] == _ POST ['security _ Code'] &! Empty ($ _ SESSION ['security _ Code']) {
// Insert you code for processing the form here, e. g emailing the submission, entering it into a database.
Echo 'thank you. Your message said "'. $ _ POST ['message'].'" ';
Unset ($ _ SESSION ['security _ Code']);
} Else {
// Insert your code for showing an error message here
Echo 'Sorry, you have provided an invalid security code ';
}
} Else {
?>

}
?>

Validators CaptchaSecurityImages. php

The code is as follows:

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 program is 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 program is distributed in the hope that it will be useful,
* But without any warranty; without even the implied warranty
* MERCHANTABILITY or fitness for a special PURPOSE. See
* 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 removed */
$ Possible = '23456789bcdfghjkmnpqrstvwxyz ';
$ Code = '';
$ I = 0;
While ($ I <$ characters ){
$ Code. = substr ($ possible, mt_rand (0, strlen ($ possible)-1), 1 );
$ I ++;
}
Return $ code;
}

Function CaptchaSecurityImages ($ width = '000000', $ height = '40', $ characters = '6 '){
$ Code = $ this-> generateCode ($ characters );
/* Font size will 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']: '20140901 ';
$ Height = isset ($ _ GET ['height'])? $ _ GET ['height']: '40 ';
$ Characters = isset ($ _ GET ['characters ']) & $ _ GET ['characters']> 1? $ _ GET ['characters ']: '6 ';

$ Captcha = new CaptchaSecurityImages ($ width, $ height, $ characters );

?>

Bytes. Copy the following code...

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.