php生成验证码与验证码验证完整实例

无标题文档

?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
< title >无标题文档</ title >
< script language = "javascript" >
function refresh_code()
{
  form1.imgcode.src="verifycode.php?a="+Math.random();
}
</ script >
</ head >
< body >
< form id = "form1" name = "form1" method = "post" action = "checkcode.php" >
  < label for = "code" >验证码:</ label >
  < input type = "text" name = "code" id = "textfield" />
  < img id = "imgcode" src = "VerifyCode.php" alt = "验证码" />
  < a href = "javascript:refresh_code()" >看不清?换一个</ a >
  < input type = "submit" name = "button" id = "button" value = "提交" />
</ form >
</ body >
</ html >

verifycode.php文件代码如下

?
<?php
/*
  图片验证码 Powered By KASON test <A href="http://www.hzhuti.com/nokia/c6/">http://www.hzhuti.com/nokia/c6/</A>   */
  session_start();
  $num =4; //验证码个数
  $width =80; //验证码宽度
  $height =20; //验证码高度
  $code = ' ' ;
  for ( $i =0; $i < $num ; $i ++) //生成验证码
  {
   switch (rand(0,2))
   {
    case 0: $code [ $i ]= chr (rand(48,57)); break ; //数字
    case 1: $code [ $i ]= chr (rand(65,90)); break ; //大写字母
    case 2: $code [ $i ]= chr (rand(97,122)); break ; //小写字母
   }
  }
  $_SESSION [ "VerifyCode" ]= $code ;
  $image =imagecreate( $width , $height );
  imagecolorallocate( $image ,255,255,255);
  for ( $i =0; $i <80; $i ++) //生成干扰像素
  {
   $dis_color =imagecolorallocate( $image ,rand(0,2555),rand(0,255),rand(0,255));
   imagesetpixel( $image ,rand(1, $width ),rand(1, $height ), $dis_color );
  }
  for ( $i =0; $i < $num ; $i ++) //打印字符到图像
  {
   $char_color =imagecolorallocate( $image ,rand(0,2555),rand(0,255),rand(0,255));
   imagechar( $image ,60,( $width / $num )* $i ,rand(0,5), $code [ $i ], $char_color );
  }
  header( "Content-type:image/png" );
  imagepng( $image ); //输出图像到浏览器
  imagedestroy( $image ); //释放资源
?>

checkcode.php文件如下

?
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<?php
ini_set ( 'display_errors' , 'Off' );
session_start();
  if (( strtoupper ( $_POST [ "code" ])) == strtoupper (( $_SESSION [ "VerifyCode" ]))){
print ( "验证码正确," );
  } else {
    print ( "验证码错误," );
  }
  echo "提交的验证码:" . strtoupper ( $_POST [ "code" ]). ",正确的验证码:" . strtoupper ( $_SESSION [ "VerifyCode" ]);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值