PHP的验证码实现

原文地址:http://www.crazyant.net/2012/08/15/php%E9%AA%8C%E8%AF%81%E7%A0%81%E6%95%99%E7%A8%8B%EF%BC%88w3schools%E6%8E%A8%E8%8D%90%EF%BC%89/


本文使用PHP一些可用的特性实现了验证码功能。该教程非常的简单,使用可以改变的字体生成了验证码图片,正如我们所了解的,验证码是用于避免垃圾评论或者自动提交的。

本验证码程序的资源:

下载地址

在线演示

captcha_code_file.php

代码:

?
<?php
session_start();
//设置: 你可以在这里修改验证码图片的参数
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf' ;
 
//以下字符将用于验证码中的字符
//为了避免混淆去掉了数字1和字母i
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz' ;
$random_dots = 10;
$random_lines = 30;
$captcha_text_color = "0x142864" ;
$captcha_noice_color = "0x142864" ;
 
$code = '' ;
 
$i = 0;
while ( $i < $characters_on_image ) {
     $code .= substr ( $possible_letters , mt_rand(0, strlen ( $possible_letters )-1), 1);
     $i ++;
}
 
$font_size = $image_height * 0.75;
$image = @imagecreate( $image_width , $image_height );
 
/* 设置背景、文本和干扰的噪点 */
$background_color = imagecolorallocate( $image , 255, 255, 255);
 
$arr_text_color = hexrgb( $captcha_text_color );
$text_color = imagecolorallocate( $image , $arr_text_color [ 'red' ],
$arr_text_color [ 'green' ], $arr_text_color [ 'blue' ]);
 
$arr_noice_color = hexrgb( $captcha_noice_color );
$image_noise_color = imagecolorallocate( $image , $arr_noice_color [ 'red' ],
$arr_noice_color [ 'green' ], $arr_noice_color [ 'blue' ]);
 
/* 在背景上随机的生成干扰噪点 */
for ( $i =0; $i < $random_dots ; $i ++ ) {
     imagefilledellipse( $image , mt_rand(0, $image_width ),
     mt_rand(0, $image_height ), 2, 3, $image_noise_color );
}
 
/* 在背景图片上,随机生成线条 */
for ( $i =0; $i < $random_lines ; $i ++ ) {
     imageline( $image , mt_rand(0, $image_width ), mt_rand(0, $image_height ),
     mt_rand(0, $image_width ), mt_rand(0, $image_height ), $image_noise_color );
}
 
/* 生成一个文本框,然后在里面写生6个字符 */
$textbox = imagettfbbox( $font_size , 0, $font , $code );
$x = ( $image_width - $textbox [4])/2;
$y = ( $image_height - $textbox [5])/2;
imagettftext( $image , $font_size , 0, $x , $y , $text_color , $font , $code );
 
/* 将验证码图片在HTML页面上显示出来 */
header( 'Content-Type: image/jpeg' ); // 设定图片输出的类型
imagejpeg( $image ); //显示图片
imagedestroy( $image ); //销毁图片实例
$_SESSION [ '6_letters_code' ] = $code ;
 
function hexrgb ( $hexstr ) {
     $int = hexdec( $hexstr );
 
     return array ( "red" => 0xFF & ( $int >> 0x10),
                 "green" => 0xFF & ( $int >> 0x8),
                 "blue" => 0xFF & $int
     );
}
?>

验证验证码正确或错误的方法

验证码图片上的文字被存放到了SESSION 变量里面,验证的时候,我们需要将SESSION 里面的值和用户输入的值进行比较即可。

$_SESSION[6_letters_code] – 存放着验证码的文字值
$_POST[6_letters_code] – 这是用户输入的验证码的内容

index.php代码

包含了HTML, CSS, Javascript and PHP代码:

?
<?php session_start();
 
if (isset( $_REQUEST [ 'Submit' ])){
     // 服务器端验证的代码
     if ( empty ( $_SESSION [ '6_letters_code' ] ) ||
         strcasecmp ( $_SESSION [ '6_letters_code' ], $_POST [ '6_letters_code' ]) != 0)
     {
         $msg = "验证失败!" ;
     } else {
         //验证码验证正确,这里放验证成功后的代码
     }
}
?>
<style type= "text/css" >
.table {
     font-family:Arial, Helvetica, sans-serif;
     font-size:12px;
     color:#333;
     background-color:#E4E4E4;
}
.table td {
     background-color:#F8F8F8;
}
</style>
 
<form action= "" method= "post" name= "form1" id= "form1" >
   <table width= "400" border= "0" align= "center" cellpadding= "5" cellspacing= "1" class = "table" >
     <?php if (isset( $msg )){?>
     <tr>
       <td colspan= "2" align= "center" valign= "top" ><?php echo $msg ;?></td>
     </tr>
     <?php } ?>
     <tr>
       <td align= "right" valign= "top" > 验证码:</td>
       <td><img src= "captcha_code_file.php?rand=<?php echo rand();?>" id= 'captchaimg' ><br>
         <label for = 'message' >请输入上面的验证码 :</label>
         <br>
         <input id= "6_letters_code" name= "6_letters_code" type= "text" >
         <br>
         无法读图片吗?点击 <a href= 'javascript: refreshCaptcha();' >here</a> 刷新
         </p></td>
     </tr>
     <tr>
       <td> </td>
       <td><input name= "Submit" type= "submit" onclick= "return validate();" value= "提交" ></td>
     </tr>
   </table>
</form>
<script type= 'text/javascript' >
function refreshCaptcha()
{
     var img = document.images[ 'captchaimg' ];
     img.src = img.src.substring(0,img.src.lastIndexOf( "?" ))+ "?rand=" +Math.random()*1000;
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值