PHP的验证码实现(w3schools推荐)

本文使用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>

 

相关推荐

9 thoughts on “PHP的验证码实现(w3schools推荐)”

  1. Hello,博主,我搭建了个php社区,希望php能有个类似ruby,node的那种社区,现在社区刚刚上线,博主能帮忙介绍下吗?如果博主有兴趣加入那就更好了!地址为:http://phpcnc.org

    回复

回复 漂亮美女 取消回复