arrow2 电子商务 arrow2 网络安全
安全的验证码
安全,验证码
2007-10-07 20:56:35  

字体         收藏 推荐 打印 关闭

原则:
图片、
位置不固定、
字母数字特殊符号混合、
带杂点、
有干扰线



<?php
/**
 * 安全验证码
 * @author 流水孟春 cmpan#qq.com
 *
 */

header("Cache-control: private");
header("content-type: image/png");
        
$length = isset($_GET['length']) ? $_GET['length'] : 4; // 校验码长度/字符


// 生成校验码

//function confirmCode($length){

$codeSet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&';
$imageX = mt_rand(1, 25);
$imageY = mt_rand(1, 25);
$imageL = $imageX + $length*10 + 4; // 图片宽度

$imageH = $imageY + 20; // 图片高度

$noiseNum = 10*$length; // 杂点数量

$lineNum = $length - 3; // 干扰线数量


// 建立一幅 $imageL x $imageH 的图像

$image = imagecreate($imageL, $imageH);

$bg = imagecolorallocate($image, 255, 255, 255); // 白色背景

$textColor = imagecolorallocate($image, 0, 0, 255); // 蓝色文本

$rectangleColor = imagecolorallocate($image, 0, 168, 255); // 边框颜色

$noiseColor = imagecolorallocate($image, 0, 0, 255); // 杂点颜色

$lineColor = imagecolorallocate($image, 0, 0, 255); // 干扰线颜色


$code = ''; // 校验码

for ($i = 0; $i<$length; $i++) $code .= $codeSet[mt_rand(0, 41)];

$_SESSION['confirmCode'] = $code; // 把校验码保存到session


imagestring($image, 5, $imageX, $imageY, $code, $textColor); // 把校验码写入图像

//imagerectangle($image, 0, 0, $imageL-1, $imageH-1, $rectangleColor); // 添加边框

//加入干扰线

for($i=0; $i<$lineNum; $i++){
    imageline($image, mt_rand(0, $imageL), mt_rand(0, $imageH),
    mt_rand(0, $imageL), mt_rand(0, $imageH), $lineColor);
}
//加入杂点

for($i=0;$i<$noiseNum;$i++)
imagesetpixel($image,mt_rand(0,$imageL),mt_rand(0,$imageH),$noiseColor);

imagepng($image); // 输出图像

imagedestroy($image);


 

 

 

-------------------------------------------------------------

 


<?php
session_start();
header("Cache-control: private");
header("content-type: image/png");
        
$length = isset($_GET['length']) ? $_GET['length'] : 4;
// 校验码长度/字符


// 生成校验码

//function confirmCode($length){

$codeSet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
//

$imageX = mt_rand(1, 25);
// 左边距

$imageY = mt_rand(1, 25);
// 上边距

$imageL = 60 + $length*10 + 4;
// 图片宽度

$imageH = 50;
// 图片高度

$noiseNum = 200*$length;
// 杂点数量

$lineNum = $length - 2;
// 干扰线数量


// 建立一幅 $imageL x $imageH 的图像

$image = imagecreate($imageL, $imageH);

$bg = imagecolorallocate($image, 255, 255, 255);
// 白色背景

$textColor = imagecolorallocate($image, mt_rand(10,100), mt_rand(50,150), mt_rand(100,200));
// 随机颜色文本

$rectangleColor = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
// 边框颜色

$noiseColor = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
// 杂点颜色

$lineColor = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
//干扰线颜色

    
$code = '';
// 校验码

for ($i = 0; $i<$length; $i++) {
    $tmp = $codeSet[mt_rand(0, 36)];
    $code .= $tmp;
    imagestring($image, 5, $imageX+$i*15, $imageY+ mt_rand(-4,4), $tmp, imagecolorallocate($image, mt_rand(100,140), mt_rand(120,150), mt_rand(150,180)));
// 把校验码写入图像

}

$_SESSION['confirmCode'] = $code;
// 把校验码保存到session


//imagerectangle($image, 0, 0, $imageL-1, $imageH-1, $rectangleColor); // 添加边框

//加入干扰线

for($i=0; $i<$lineNum; $i++){
    imageline($image, mt_rand(1, $imageL), mt_rand(1, $imageH),    mt_rand(1, $imageL), mt_rand(1, $imageH), $lineColor);
}
//加入杂点

for($i=0;$i<$noiseNum;$i++)
imagesetpixel($image,mt_rand(1,$imageL),mt_rand(1,$imageH),imagecolorallocate($image, mt_rand(200,255), mt_rand(180,255), mt_rand(190,255)));

imagepng($image);
// 输出图像

imagedestroy($image);

?>


来自:不详   作者: 未知   责任编辑:admin | 返回顶部


用户评论
文章分类


名称 作者 内容 论坛

关键字
安全,验证码

google中搜索
相关文章
  · 保障电子商务 ...
  · 什么是网络安 ...
  · PHP 应用程序 ...
  · PHP 的安全错误
  · 图片验证码程 ...
  · C#生成中文汉 ...
  · Asp.Net 动态生 ...

最新发表
  ·  C# 打造个性浏览器
  ·  域名解析操作说明
  ·  W2K下安装MYSQL 4.0.21
  ·  Asp.net中执行Url重写
  ·  第三方Web Html Editor的使用
  ·  Iframe跨域问题.
  ·  垃圾邮件评判优化
  ·  如何对电子商务系统进行需求分析
  ·  IPS支付说明
  ·  谈谈PHP文章系统和SEO的关系