首页 > 编程技术 > php

php高安全验证码生成程序

发布时间:2016-11-25 15:24

本生成程序需要调用一些字体库哦,你可以调用你系统自带的一些字体,当然也可以像dedecms一样自己把字体放到一个目录,这样在服务器上也可以使用了。

 

 代码如下 复制代码

<?php
public function captcha(){
$font_dir = $_SERVER ["DOCUMENT_ROOT"] . "your_ttf_file.ttf"; // 字体库
$img_w = 58; // 设置图片宽
$img_h = 20; // 设置图片高
$font_size = 11; // 字体大小
$angle_l = -10; // 左偏角
$angle_r = 10; // 右偏角
$code_str = "ABCDEFGHJKLMNPQRSTUVWXYZ36";
$word_len = 4; // 验证码位数
$padding = 5; // 每两个文字之间间隔
$margin = 2; // 左侧边距
$base_line = 15; // 文字基线位置
$base_line_offset = 2; // 基准线偏移量
$pixel_num = 3; // 杂点数目基数
$pixel_color= 8; // 杂点只有 $pixel_color 种颜色 总的杂点数为$pixel_num*$pixel_color
$noise_font_size = 1; // 杂点字体大小
$session_key= "my.xoyo_captcha"; //自定义session键名

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
header("Cache-control: private");
header('Content-Type: image/png');

session_start();
$word = "";
$code_str_len = strlen($code_str) - 1;
for ($i = 0; $i < $word_len; $i++) {
$word.= $code_str[rand(0, $code_str_len)];
}
$_SESSION [$session_key] = strtolower($word);
$image = imagecreatetruecolor($img_w, $img_h);
imagefilledrectangle($image, 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate($image, mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255)));

//绘制杂点
for($i = 0; $i < $pixel_color; $i++){
$noise_color = imagecolorallocate( $image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) );
for($j = 0; $j < $pixel_num; $j++) {
imagestring( $image, $noise_font_size, mt_rand(-10, $img_w), mt_rand(-10, $img_h), $code_str[mt_rand(0, $code_str_len)], $noise_color );
}
}

//绘制文字
for ($i = 0; $i < $word_len; ++$i) {
$color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
imagettftext($image, $font_size, mt_rand($angle_l, $angle_r), $margin, mt_rand($base_line-$base_line_offset, $base_line+$base_line_offset), $color, $font, mb_substr($word, $i, 1, 'utf-8'));
$margin += (imagefontwidth($font_size) + $padding);
}

imagepng($image);
imagedestroy($image);
exit;
}

本文章分享的这个php加密类是一个可以支持中文和英文的可加密码可解密的php实现类文件,有需要的同学可以参考一下,不过最好把文档编码设置为utf-8哦。

下面代码保存成MD5Crypt.class.php文件

 代码如下 复制代码
<?php
class MD5Crypt {
/**
* Enter description here ...
* @param unknown_type $str
* @return string
*/
public final static function mdsha($str) {
$code = substr ( md5 ( $str ), 10 );
$code .= substr ( sha1 ( $str ), 0, 28 );
$code .= substr ( md5 ( $str ), 0, 22 );
$code .= substr ( sha1 ( $str ), 16 ) . md5 ( $str );
return self::chkToken () ? $code : null;
}
/**
* Enter description here ...
* @param unknown_type $param
*/
private final static function chkToken() {
return true;
}
/**
* Enter description here ...
* @param unknown_type $txt
* @param unknown_type $encrypt_key
* @return Ambigous <string, boolean>
*/
private final static function keyED($txt, $encrypt_key) {
$encrypt_key = md5 ( $encrypt_key );
$ctr = 0;
$tmp = "";
for($i = 0; $i < strlen ( $txt ); $i ++) {
if ($ctr == strlen ( $encrypt_key ))
$ctr = 0;
$tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 );
$ctr ++;
}
return $tmp;
}
/**
* Enter description here ...
* @param unknown_type $txt
* @param unknown_type $key
* @return string
*/
public final static function Encrypt($txt, $key) {
srand ( ( double ) microtime () * 1000000 );
$encrypt_key = md5 ( rand ( 0, 32000 ) );
$ctr = 0;
$tmp = "";
for($i = 0; $i < strlen ( $txt ); $i ++) {
if ($ctr == strlen ( $encrypt_key ))
$ctr = 0;
$tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ));
$ctr ++;
}
$_code = md5 ( $encrypt_key ) . base64_encode ( self::keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key );
return self::chkToken () ? $_code : null;
}
/**
* Enter description here ...
* @param unknown_type $txt
* @param unknown_type $key
* @return Ambigous <string, boolean>
*/
public final static function Decrypt($txt, $key) {
$txt = self::keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key );
$tmp = "";
for($i = 0; $i < strlen ( $txt ); $i ++) {
$md5 = substr ( $txt, $i, 1 );
$i ++;
$tmp .= (substr ( $txt, $i, 1 ) ^ $md5);
}
return self::chkToken () ? $tmp : null;
}
/**
* Enter description here ...
* @var unknown_type
*/
private static $_key = 'lau';
}
?>

用法

 

 代码如下 复制代码
<?php //Code Start
define ( 'WORKSPACE', '.' . DIRECTORY_SEPARATOR );
header ( "Content-Type: text/html; charset=utf-8" );
include_once 'Core/Library/MD5Crypt.class.php';
$a = MD5Crypt::Encrypt ( "A", 100 );
echo "EnCode:" . $a, "<br />";
echo "DeCode:" . MD5Crypt::Decrypt ( $a, 100 );
?>
一般在用验证码时我们都要实现局部刷新了,这个是必然的,也是提高用户体验的一点,下面我们来看一个php验证码在实际应用中的用法,有需要的朋友来参考一下。

/*
 * Auth_code()是验证码函数
 * @access public
 * @param int $width 表示验证码的长度,默认为80
 * @param int $height 表示验证码的高度,默认为20
 * @param int $num  表示验证码中数字位数,默认为4
 * @param int $line  表示验证码中线的条数,默认为4
 * @param int $line  表示验证码中点的个数,默认为150
 */

 代码如下 复制代码

function Auth_code($width = 80,$height = 20,$num = 4,$line = 4,$dot = 150) {

 $length = floor($width/$num);  //floor:取整数部分 length:每段平均长度
 for($i=0;$i<$num;$i++)
  @$rand.=dechex(mt_rand(1,15));
 $_SESSION['code']=$rand;
 
 //新建一个黑色底的画板
 $im=imagecreatetruecolor($width,$height);
 
 //取色
 $green=imagecolorallocate($im,0,255,0);
 $red = imagecolorallocate($im,255,0,0);
 $white=imagecolorallocate($im,255,255,255);
 $black = imagecolorallocate($im,0,0,0);
 
 //填充画板
 //imagefill($im,0,0,$green);
   
 //画线
 for($i=0;$i<$line;$i++){
  $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imageline($im,rand(10,$width-10),0,rand(0,$width),$height,$color);
 }
 //画点
 for($i=0;$i<$dot;$i++){
  $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imagesetpixel($im,rand(0,$width),rand(0,$height),$color);
 }
 //画数字
 for($i=0;$i<$num;$i++) { 
  imagestring($im,5,mt_rand($i*$length+1,($i+1)*$length-8),mt_rand(0,$height-14),$_SESSION['code'][$i],$white);
 }
 
 //输出图片
 header("content-type:image/jpeg");
 imagejpeg($im);
}

 -主要函数:
       imagecreatetruecolor  //新建一个黑色底的画板
       imagecolorallocate     //取色
       imagefill                     //填充画板
       imageline                  //画线
       imagesetpixel           //画点
       imagestring             //画字符串(本验证码为数字)
       注意:在将数字验证码画上画板时要注意字体越界,所以要相应减去一些范围,详见附件。
    -局部刷新验证码:

 代码如下 复制代码
       <img" width=100% src="code.php" onclick="javascript:this.src='code.php?id='+Math.random()">
分享一款比较好的php验证码程序,有需要的朋友参考一下。
 代码如下 复制代码

代码如下:
<?php
   /*   网站验证码程序
    *   运行环境: PHP5.0.18 下调试通过
    *   需要 gd2 图形库支持(PHP.INI中 php_gd2.dll开启)
    *   文件名: showimg.php
    *   作者:  17php.com
    *   Date:   2007.03
    */

   //随机生成一个4位数的数字验证码
    $num="";
    for($i=0;$i<4;$i++){
    $num .= rand(0,9);
    }
   //4位验证码也可以用rand(1000,9999)直接生成
   //将生成的验证码写入session,备验证页面使用
    Session_start();
    $_SESSION["Checknum"] = $num;
   //创建图片,定义颜色值
    Header("Content-type: image/PNG");
    srand((double)microtime()*1000000);
    $im = imagecreate(60,20);
    $black = ImageColorAllocate($im, 0,0,0);
    $gray = ImageColorAllocate($im, 200,200,200);
    imagefill($im,0,0,$gray);

    //随机绘制两条虚线,起干扰作用
    $style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);
    imagesetstyle($im, $style);
    $y1=rand(0,20);
    $y2=rand(0,20);
    $y3=rand(0,20);
    $y4=rand(0,20);
    imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);
    imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED);

    //在画布上随机生成大量黑点,起干扰作用;
    for($i=0;$i<80;$i++)
    {
   imagesetpixel($im, rand(0,60), rand(0,20), $black);
    }
    //将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
    $strx=rand(3,8);
    for($i=0;$i<4;$i++){
    $strpos=rand(1,6);
    imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black);
    $strx+=rand(8,12);
    }
    ImagePNG($im);
    ImageDestroy($im);
   ?>


使用方法:
 本程序可以直接运行,运行之后即可看到一个图形验证码,每次刷新都随机生成新码。
 在某页面中使用此程序时,可以用以下代码:
代码如下:
.....
 请输入验证码:

 代码如下 复制代码
<input type=text name=passcode>
<img" width=100% src=showimg.php>
.....

这样即可显示出验证码图片。到了验证页面,用以下代码:
代码如下:
...

 代码如下 复制代码
$code=$_POST["passcode"];
if( $code == $_SESSION["Checknum"]){
验证通过
}else{
验证码错误
}
...
我们举例只讲linux的系统,但是防止方法在任何系统都是有效的,下面我们先来看看等操作


你可以这样使用

 代码如下 复制代码

http://www.xxx.com/index.php?page=../etc/passwd
http://www.xxx.com/index.php?page=../../../etc/passwd
http://www.xxx.com/index.php?page=..../../etc/passwd

获取更多数据:
etc/profile
etc/services
/etc/passwd
/etc/shadow
/etc/group
/etc/security/group
/etc/security/passwd
/etc/security/user
/etc/security/environ
/etc/security/limits
/usr/lib/security/mkuser.default


像上面代码,如果你是

?page=$_GET的话这样就完了,分析原因,因为我们分页只有数字,那么我们这样操作

 代码如下 复制代码

?page=intval($_GET);

这样就无法把字符提交了,我们利用了intval函数进行了过滤,那么对于提交字符怎么处理呢。

在处理字符时我们利用php自带函数的函数 addslashes和htmlspecialchars进行过滤,

 代码如下 复制代码

$body = htmlspecialchars(isset($_GET[$str])?$_GET[$str]:'');

这样就基本过滤了各种安全注入,当然如果你服务器有漏洞在php上是解决不了的。

标签:[!--infotagslink--]

您可能感兴趣的文章: