很实用的一个php验证码类

 更新时间:2016年11月25日 15:25  点击:1554
一个比较实用的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中怎么去应用中文验证码,中文因为是汉字可能出现乱码所以我们就定义了一个文件来专门处理,有需要的朋友可以参考下。
 代码如下 复制代码


<?php   
/*   
* 文件:chinesechar.php   
* 作用:汉字数据储存   
*/   
$ChineseChar = array("人","出","来","友","学","孝","仁","义","礼","廉","忠","国","中","易","白","者","火 ","土","金","木","雷","风","龙","虎","天","地",   "生","晕","菜","鸟","田","三","百","钱","福 ","爱","情","兽","虫","鱼","九","网","新","度","哎","唉","啊","哦","仪","老","少","日",   "月 ","星");   
?>   

<?php   
/*   
* 文件:check.php   
* 作用:验证   

*/   
session_start();   
$errorMSG = '';   
//验证用户输入是否和验证码一致   
if(!is_null($_POST['check']))   
{   
        if (strcasecmp($_SESSION['code'],$_POST['code'])==0)   
            $errorMSG = "<p style="font-size:12px;color:#009900">验证成功!</p>";   
        else   
            $errorMSG = "<p style="font-size:12px;color:#FF0000">验证失败!</p>";   
}   
?>   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
</head>   
<body>   
<?php   
if($errorMSG){   
echo $errorMSG;   
}   
?>   
<form action=<?php echo $_SERVER['PHP_SELF']?> method=post>   
请输入验证码:<input type="text" name="code" style="width:   
80px"><img src="code.php">   
<br>   
<input type="submit" name="check" value="提交验证码">   
</form>   
</body>   
</html>   

<?php   
/*   
* 文件:code.php   
* 作用:验证码生成   
 
* 特注:由 没牙的草 指导 版权所有转载注明出处!有付出才会有收获! 
*/   
include_once("chinesechar.php");   
session_start();   
// 设置 content-type   
header("Content-type: image/png");   
// 创建图片   
$im = imagecreatetruecolor(120, 30);   

// 创建颜色   
$fontcolor = imagecolorallocate($im, 255, 255, 255);   
$bg = imagecolorallocate($im, 0, 0, 0);   

// 设置文字   
for($i=0;$i<4;$i++) $text .= $ChineseChar[(array_rand($ChineseChar))];   

$_SESSION['code'] = $text;   
// 设置字体   
$font = 'simkai.ttf';   

// 添加文字   
imagettftext($im, 18, 0, 11, 21, $fontcolor, $font, iconv("GB2312","UTF-8",$text));   

// 输出图片   
imagepng($im);   
imagedestroy($im);   
?>

如果想把上面的程序改成英文数字,只要在chinesechar.php 里面的数组中文改成数字或字母就KO了。

一款国外网站下载的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开发者都会使用这个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);
*/

?>

[!--infotagslink--]

相关文章

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

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • Django def clean()函数对表单中的数据进行验证操作

    这篇文章主要介绍了Django def clean()函数对表单中的数据进行验证操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-09
  • jQuery Real Person验证码插件防止表单自动提交

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

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

    这篇文章主要为大家详细介绍了JavaScript实现密码框输入验证,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-01
  • js实现跨域的4种实用方法原理分析

    什么是js跨域呐?js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据。只要协议、域名、端口有任何一个不同,都被当作是不同的域。要...2015-10-30
  • Nest.js 授权验证的方法示例

    这篇文章主要介绍了Nest.js 授权验证的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-22
  • js实现跨域的4种实用方法原理分析

    什么是js跨域呐?js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据。只要协议、域名、端口有任何一个不同,都被当作是不同的域。要...2015-10-30
  • Jquery插件实现点击获取验证码后60秒内禁止重新获取

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

    验证码类文件 CreateImg.class.php <&#63;php class ValidationCode { private $width,$height,$codenum; public $checkcode; //产生的验证码 private $checkimage; //验证码图片 private $disturbColor = ''; /...2015-11-08
  • el-table树形表格表单验证(列表生成序号)

    这篇文章主要介绍了el-table树形表格表单验证(列表生成序号),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-01
  • 基于JavaScript实现验证码功能

    这篇文章主要介绍了基于JavaScript实现验证码功能的相关资料...2017-04-03
  • selenium 反爬虫之跳过淘宝滑块验证功能的实现代码

    这篇文章主要介绍了selenium 反爬虫之跳过淘宝滑块验证功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-27
  • vue element table中自定义一些input的验证操作

    这篇文章主要介绍了vue element table中自定义一些input的验证操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-18
  • js canvas实现滑块验证

    这篇文章主要为大家详细介绍了js canvas实现滑块验证,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • vue实现表单验证小功能

    这篇文章主要为大家详细介绍了vue实现表单验证小功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-29
  • 单击按钮发送验证码,出现倒计时的简单实例

    下面小编就为大家带来一篇单击按钮发送验证码,出现倒计时的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 代码...2017-07-06
  • Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法

    这篇文章主要介绍了Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • 基于Pytorch版yolov5的滑块验证码破解思路详解

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