php生成验证码图片程序

 代码如下 复制代码

<?php
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 {
?>

 <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="30" name="message" id="message"></textarea><br />
  <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><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
 }
?>

验证程序 CaptchaSecurityImages.php

 代码如下 复制代码

<?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 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 of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 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 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='120',$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'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

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

?>

时间: 2024-07-31 03:26:40

php生成验证码图片程序的相关文章

php 验证码图片 程序

<?php php 验证码图片 程序 /* *文件名:class.safeCode.php *类名:safeCode *目的:生成web应用时所需的验证码图片 *当前版本:1.0.2 *作者:NoAngels *联系方式:flare_1023@163.com QQ:82535599 MSN:atizu@hotmail.com *开发时间:2008年06月20日 *最后更新:2008年06月21日 *更新内容: *版本1.0.2 *1.更新了对linux主机支持,设置GDFONTPATH环境变量,

使用ashx文件生成验证码图片

 在vs2005中可以直接创建.ashx文件,其项目叫做一般处理程序,.ashx文件一般用来处理只有返回,二一般不回传的数据,比如动态生成图片,或者文字,以下为清清月儿博客上转载的一个用ashx动态生成验证码图片的代码. //绘制验证码图片ValidateImageHandler.ashx 1 <%@ WebHandler Language="C#" Class="ValidateImageHandler" %> 2  3 using System; 4

php生成验证码图片从入门和精通教程

在php中要生成验证码图片是相当的简单的,因为在php中为我们提供了图形gd.dll库,要启用gd图形库我们只要在在php.ini中把php-gd前面的;去就可以了. 方法一 $authnum=''; $ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; $list=explode(",",$ychar);//分割函数 for($i=0;$i<4;$i

C语言实现用随机字符串生成验证码图片

问题描述 C语言实现用随机字符串生成验证码图片 就是正常的验证码逻辑--随机生成一个四位字符串,然后用这个字符串加随机干扰像素随机位置随机颜色之类的信息生成一张验证码图片!!!求大神帮忙········(新人暂无法悬赏,后期一定补上) 解决方案 随机生成UUID的方法可以结合使用 #include <stdio.h> #include <stdlib.h> /** * Create random UUID * * @param buf - buffer to be filled w

WinForm生成验证码图片的方法_C#教程

本文实例讲述了WinForm生成验证码图片的方法.分享给大家供大家参考,具体如下: 1.创建ValidCode类: public class ValidCode { #region Private Fields private const double PI = 3.1415926535897932384626433832795; private const double PI2 = 6.283185307179586476925286766559; //private readonly int

Javaweb开发中通过Servlet生成验证码图片_java

一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 package gacl.response.study; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; i

php网页生成验证码实现程序

完整实例 自动检测背景和字体,并随机选取背景图片中的一块范围,随机使用字体,显示验证字符串时随机显示字体大小,字符间距,字符颜色等  代码如下 复制代码 <?php /** * Class for Validate image * @author  zcs * @version 1.0-20090828 */ session_start(); class validimg { //背景图片目录 var $backgroundpath = 'validbg'; //生成验证码宽度 var $wid

PHP利用GD库画图和生成验证码图片

首先得确定php.ini设置有没有打开GD扩展功能,测试如下 print_r(gd_info()); 如果有打印出内容如下,则说明GD功能有打开: Array ( [GD Version] => bundled (2.0.34 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => 1 [GIF Read Support] => 1 [GIF Crea

加入干扰象素的生成验证码图片

        Header("Content-type: image/PNG");          srand((double)microtime()*1000000);         $im = imagecreate(58,25);         $black = ImageColorAllocate($im, 0,0,0);         $white = ImageColorAllocate($im, 255,255,255);         $gray = Ima