php生成验证码图片程序

 更新时间:2016年11月25日 15:25  点击:1706
一款国外网站下载的php生成验证码图片代码,这个比较实用我们还举例说明了,有需要的朋友按上面保存成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);

?>

一个比较实用的php图形验证码生成类,调用方法也很简单的,有需要的朋友可以参考一下。
 代码如下 复制代码

<?php

class ImageCode{
 private $width;//验证码图片宽度
 private $height;//验证码图片高度
 private $codeNum;//验证码字符个数
 private $checkCode;//验证码字符
 private $image;//验证码画布
 function __construct($width=60,$height=20,$codeNum=4){
  $this->width=$width;
  $this->height=$height;
  $this->codeNum=$codeNum;
  $this->checkCode=$this->createCheckCode();
 }
 
 function getcreateImage(){
  $this->getcreateImage();
  $this->outputText();
  $this->setDisturbColor();
  $this->outputImage();
 }
 function getCheckCode(){
  return $this->checkCode;
 }
 
 private function getCreateImage(){
  $this->image=imagecreatetruecolor($this->width,$this->height);
  $black=imagecolorallocate($this->image,255,255,255,0);
  $border=imagecolorallocate($this->image,255,255,255,255);
  imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
 }

 private function createCheckCode(){
  for($i=0;$i<$this->codeNum;$i){
   $number=rand(0,2);
   switch($number){
    case 0:
     $rand_number=rand(48,57);//数字
     break;
    case 1:
     $rand_number=rand(65,90);//大写字母
     break;
    case 2:
     $rand_number=rand(97,122);
     break;
   }
   $asc=sprintf("%c",$rand_number);
   $asc_number=$asc_number.$asc;
  }
  return $asc_number;
 }

 private function setDisturbColor(){
  for($i=0;$i<=100;$i++){
   $color=imagecolorallocate($this->image,255,255,255);
   imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  }
 }

 private function outputImage(){
  if(imagetypes()&IMG_GIF){
   header("Content_type:image/gif");
   imagegif($this->image);
  }elseif(imagetypes()&IMG_JGP){
   header("Content_type:image/jpeg");
   imagejpeg($this->image,"",0.5);
  }else{
   die("PHP不支持图像创建");
  }
 }

 function __destruct(){
  imagedestroy($this->image);
 }
}

?>

很多php开发者都会使用这个md()加密函数,但其中的参考有些朋友未必知道了,今天我们来看看md5()函数的参数吧.

语法
md5(string,raw)

md5() 函数计算字符串的 MD5 散列


string 必需。规定要计算的字符串

charlist

可选。规定十六进制或二进制输出格式:
TRUE - 原始 16 字符二进制格式
FALSE - 默认。32 字符十六进制数
注释:该参数是 PHP 5.0 中添加的。
 

 代码如下 复制代码

<?
$str ="123456";
echo 'md5 16位'.md5($str,true);

//输出乱码

//如果你想得到php5中md5( "abc ",   true)这样的返回值,那么可以:
function   bin_md5($val)   {
    return   pack( "H32 ",md5($val));
}

 

echo '<br />md5 32位'.md5($str);

//10adc3949ba59abbe56e057f20f883e
?>

自己写的一款sql注入检测函数,可以有效的检测用户post,get过来的参考进行过滤,有需要的朋友参考一下。
 代码如下 复制代码

<?php
/*sql 注入 字符的检测
* 在所有用户输入的数据,post传参, get传参 都需要检测下 
* 如果有匹配到关键字 则 返回该关键字  否则返回false
* 这个和敏感字符的检测不是一样的
*/
function Filter_SQL($strData)
{
 $strFilter=$blnFlag=$arrayFilter='';

 $strFilter="'|and|(|)|exec|insert|select|delete|update|count|*|%27|chr|mid|master|truncate|char|declare|union|or"; //需要过滤的字符,可以自己添,"|"是分隔符
 $blnFlag=false;   //过滤标志,如果产生过滤,那么就是真
 
 $arr=explode("|",$strFilter); 
 $str="";
 
 foreach($arr as $row)
 {
  $str.=preg_quote($row)."|";
 } 
 
 $str=trim($str,"|"); 
 
 if(preg_match('/'.$str.'/i',$strData,$word))
 {
  return $word[0];
 }
 
 return false;
}

/*
测试
$string="fasdf union ";
echo Filter_SQL($string);
*/

?>

这里介绍了一款安全性比较高的验证生成程序,可以带干扰线等内容,可以有效的防止用户用程序识别验证码的难度了。
 代码如下 复制代码

<?php教程
/*
 * Created on 2011-3-11
 * Programmer : xiaoyao, QQ:1045195056
 验证通过判断输入值与$_SESSION['check_pic']值
 */
session_start();
 function RandAscii($number){//$number产生数字和字母个数
$arr=array('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','w','v','u','x','y','z');
for ($i=1;$i<=$number;$i++)
{
$rand= $rand.$arr[rand(0,35)];
}
return $rand;
}
 $rand=RandAscii(4);

$_SESSION['check_pic']=$rand;//随机产生的四个数赋值session中,用于验证。
$x=80;
$y=24;
 $im=imagecreatetruecolor($x,$y);//创建图片
$bg=imagecolorallocate($im,255,255,255);//设置颜色背景
imagefill( $im,0,0,$bg);
$wh=imagecolorallocate($im,255,255,0);
$grey=imagecolorallocate($im,128,128,128);
$yellow=imagecolorallocate($im,255,255,0);
$red=imagecolorallocate($im,0,255,0);
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
  imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
  imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
  imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
 );//字颜色数组
 //画边框
 $border = imagecolorallocate($im, 133, 153, 193);
 imagerectangle($im, 0, 0, $x - 1, $y - 1, $border);

for($i=0;$i<10;$i++){        //画干扰线,10条
imageline($im,rand(0,60),2,rand(0,60),20,$yellow);

}
for($j=0;$j<100;$j++){
 imagesetpixel($im,rand()%76,rand()%20,$red);
}
//imagestring($im,6,15,8,$rand,$wh);//字体大小1-5
imagettftext($im, 14,rand(30, -30), 5, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[0]);
imagettftext($im, 14,rand(50, -50), 24, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[1]);
imagettftext($im, 14,rand(50, -50), 43, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[2]);
imagettftext($im, 14,rand(30, -30), 62, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[3]);

header("Content-type: image/jpeg");//输出图片
imagejpeg($im);
imagedestroy($im);
?>

调用方法

 代码如下 复制代码
<?php
/*
 * Created on 2011-3-11
 * Programmer : xiaoyao, QQ:1045195056
 验证通过判断输入值与$_SESSION['check_pic']值
 */
session_start();//开启session
if(isset($_POST['check']))
{
if($_POST['check'])
 {
if($_POST['check']==$_SESSION['check_pic'])
 {
 echo " 验证码正确".$_SESSION['check_pic'];
 }
else
 {
 echo " 验证码错误".$_SESSION['check_pic'];
 }
}
}
?>
<FORM METHOD=POST ACTION="">
<img src="index.php"><br>    <!----链接图片--->
<input type="text" name="check" >
<input type="submit" value="提交">
</FORM>

 

[!--infotagslink--]

相关文章

  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • jQuery Real Person验证码插件防止表单自动提交

    本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08
  • JS实现随机生成验证码

    这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • Jquery插件实现点击获取验证码后60秒内禁止重新获取

    通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能效果图:先到官网(http://plugins.jquery.com/cookie/)下载cookie插件,放到相应文件夹,代码如下:复制代码 代码如下: <!DOCTYPE ht...2015-03-15
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • PHP自动生成后台导航网址的最佳方法

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
  • php实现点击可刷新验证码

    验证码类文件 CreateImg.class.php <&#63;php class ValidationCode { private $width,$height,$codenum; public $checkcode; //产生的验证码 private $checkimage; //验证码图片 private $disturbColor = ''; /...2015-11-08
  • 基于JavaScript实现验证码功能

    这篇文章主要介绍了基于JavaScript实现验证码功能的相关资料...2017-04-03
  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
  • Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法

    这篇文章主要介绍了Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24
  • 单击按钮发送验证码,出现倒计时的简单实例

    下面小编就为大家带来一篇单击按钮发送验证码,出现倒计时的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 代码...2017-07-06
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • PHP生成不同颜色、不同大小的tag标签函数

    复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
  • 基于Pytorch版yolov5的滑块验证码破解思路详解

    这篇文章主要介绍了基于Pytorch版yolov5的滑块验证码破解思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-25
  • jQuery实现发送验证码控制按钮禁用功能

    最近接到新需求,需要实现一个点击发送验证码之后,按钮禁用,在5秒之后取消禁用,看似需求很简单,实现起来还真的好好动动脑筋,下面小编把jquery控制按钮禁用核心代码分享给大家,需要的朋友参考下吧...2021-07-24
  • 工信部的ICP备案网站登录时验证码一直输入不正确怎么回事

    工信部的ICP备案网站登录时验证码一直输入不正确怎么回事,为了防止一些机器采集人工信部对于查询验证做得识别度极低,所以许多的朋友都会发现输入验证码一直有问题了,那...2016-10-10