Warning: session_start() [function.session-start]: open(/home/mybwtech/public_html/article/tmp/sess_891bc5e80f7981dca3528db669f73c7a, O_RDWR) failed: No such file or directory (2) in /home/mybwtech/public_html/article/global.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/mybwtech/public_html/article/global.php:3) in /home/mybwtech/public_html/article/global.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/mybwtech/public_html/article/global.php:3) in /home/mybwtech/public_html/article/global.php on line 3
php图片验证码技术 - 龙舞天翔资源共享
我的控制台 会员登陆 免费注册 最后更新 高级搜索 返回首页 我要投稿 退出登陆 龙舞论坛
当前在线: 0
 
站内搜索
Google
www.bwtech.net

德州扑克资料
投资透视
网站优化推广&SEO
网站设计
网络编程语言与开发
课件开发制作
网络文学
小资生活
数码摄影
财经-经营管理
其他杂项
龙舞天翔资源共享 / 网络编程语言与开发 / PHP语言技术 / php图片验证码技术
php图片验证码技术
2004-12-17          点击: 2751
php图片验证码技术

现在越来越多的网站用到了验证码技术,而且是随即生成图片,这样的技术在某种程度上可以阻止一定使用不法手段的攻击,盗取账号等等的不法分子!~

好了,还是开是讲讲php中这种技术的实现吧!

其实,在php中,这种技术实现很简单,由于21e.cn
的大多数版面都开发成了php版本,所以也想到用一个验证码技术,就研究了一下,其思路大致是这样的,随即生成一个随机数,大多为4位数字和字母,或者是数字和字母的组合,生成以后,用GD库的支持生成一张根据随机数来确定的图片,把随机数写入到session中,传递到要验证的页面,生成的图片显示给登陆着,并要求登陆者输入该随机数内容,提交到验证页面,验证session的内容和提交的内容是否一致,这就是大致的思路!

代码大致如下:
<?
/*
$width :图片宽
$height : 图片高
$space : 字符间距(中心间距)
$size : 字符大小(1-5)
$line_num : 干扰线条数
$length : 字符串长度
$sname :SESSION名称(供下一步验证之用)
*/
session_start();
creat_code(45,15,10,3,5,5,"code_str");

function creat_code($width, $height, $space, $size, $line_num, $length, $sname="") {


  $left = 3; // 字符左间距
  $top = 1;// 右间距
  $move = 3; //上下错位幅度

  // 生成字符串
  srand((double)microtime()*1000000);
  $possible = "0123456789"."ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $authstr = "";
  while(strlen($authstr) < $length) {
  $authstr .= substr($possible, rand(0,strlen($possible)), 1);
  }
 
  // 设置SESSION
  if ($sname != "") {
  $_SESSION[$sname] = $authstr;
  }
  // 初始化图片
  $image = imagecreate($width,$height);
 
  // 设定颜色
  $black = ImageColorAllocate($image, 0, 0, 0);
  $white = ImageColorAllocate($image, 255, 255, 255);
  $gray = ImageColorAllocate($image, 80, 80, 80);
  $silver = ImageColorAllocate($image, 240, 240, 240);
  $msilver = ImageColorAllocate($image, 220, 220, 220);
  $bg_white = ImageColorAllocate($image, 255, 255, 255);

  // 生成背景
  imagefill($image,0,0,$gray);


  // 画出字符
  for ($i = 0; $i < strlen($authstr); $i++) {
  $y = ($i%2)*$move + $top;
  imagestring($image, $size, $space*$i+$left, $y, substr($authstr,$i,1), $white);
  }
 
  // 画出横向干扰线
  if ($i > 0) {
  $line_space = ceil($height/($line_num+2));
  for ($i = 1; $i <= $line_num; $i++) {
  $y = $line_space*$i;
  imageline($image, 0, $y, $width, $y, $silver);
  }
  }

  // 输出图象
  Header("Content-type: image/PNG");
  ImagePNG($image);
  ImageDestroy($image);
}
?>

以上是生成了一个随即的数字和字母混合的4位验证码图片,我们只需要在要调用的地方调用这个图片即可:
<img src="yanzheng.php" width="50" height="18" border=0 alt="">

其中yanzheng.php即上面的程序的名称!


也可以使用纯数字的验证码技术,方法一样!
/////////纯数字的///////////////////

$num = mt_rand(1000,9999);
$im = imagecreate(50,18);
$black = ImageColorAllocate($im,0,0,0);
$white = ImageColorAllocate($im,255,255,255);
$gray = ImageColorAllocate($im,200,200,200);
imagefill($im,68,30,$gray);
imagestring($im, 5, 8, 2, $num, $white);
for($i=0;$i<100;$i++) //加入干扰象素
{
imagesetpixel($im, rand()%70 , rand()%30 , $gray);}
ImagePNG($im);
ImageDestroy($im);

至于传递等等,由你自己实现吧!~

责任编辑: 龙舞天翔

相关文章
关于phpArticle后台无法登陆的问题 - 02-24 10:23 am - 点击: 481
php 实现无限分类 - 07-14 01:25 am - 点击: 1496
ASP,PHP,JSP各自的优点和缺点 - 03-05 10:47 pm - 点击: 2068
提升PHP速度全攻略 - 02-09 04:42 am - 点击: 1379
PHP:路在何方? - 02-09 04:37 am - 点击: 1425
PHP编码规范 - 02-09 04:33 am - 点击: 1266
PHP简介 - 02-09 04:31 am - 点击: 1679
回顾 PHP 5.0 的变化与PHP 6.0 展望 - 02-08 10:23 pm - 点击: 1448
phpArticle 首页分类调用较完美解决方案 - 12-14 05:07 am - 点击: 1577
phpArticle防止图片盗链的方法 - 12-11 11:04 am - 点击: 1616

发表评论 查看评论 加入收藏 Email给朋友 打印本文
如果你想对该文章评分, 请先登陆, 如果你仍未注册,请点击注册链接注册成为本站会员.
平均得分 0, 共 0 人评分
1 2 3 4 5 6 7 8 9 10
Copyright © 2002 -2003 龙舞天翔资源共享
All rights reserved.
Powered by: phpArticle Version 2.0

Warning: Unknown: open(/home/mybwtech/public_html/article/tmp/sess_891bc5e80f7981dca3528db669f73c7a, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/home/mybwtech/public_html/article/tmp) in Unknown on line 0