在生成验证码时,使用类来生成,并能通过生成方法返回生成的验证码字符串,将之保存到cookie中,能过JS来获取COOKIE中的验证码,这样在前端就可以进行验证了。
一、创建生成验证码的类clsCode.class.php
<?php
class clsCode {
function __construct($char_len=5, $img_w=70 , $img_h=20 , $font = 8){
$this->char = array_merge ( range ( 'a', 'z' ), range ( 'A', 'Z' ), range ( '1', '9' ) );
$this->char_len=$char_len;
$this->img_w=$img_w;
$this->img_h=$img_h;;
$this->font=$font;
}
function output() {
$rand_keys = array_rand ( $this->char, $this->char_len ); //随机从数组中取指定个数的元素,生成键值
if ($this->char_len == 1) { //若只有一个数,则array_rand()返回非数组类型
$rand_keys = array ($rand_keys );
}
shuffle ( $rand_keys ); //可以不用
$code = '';
foreach ( $rand_keys as $k ) {
$code .= $this->char [$k];
}
session_start ();
$_SESSION ['captcha'] = $code;
//使用cookie,以例前台用JS存取
//设置cookie以便前台JS中获取验证码
//setcookie('captcha', $code, time() + 3600) ;
//添加线、色
//创建新图像
$img = imagecreatetruecolor ( $this->img_w, $this->img_h );
//分配颜色
$bg_color = imagecolorallocate ( $img, 0xcc, 0xcc, 0xcc );
//画布背景色
imagefill ( $img, 0, 0, $bg_color );
//干扰线
for($i = 0; $i < 100; ++ $i) {
$color = imagecolorallocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
imagesetpixel ( $img, mt_rand ( 0, $this->img_w ), mt_rand ( 0, $this->img_h ), $color );
}
// for($i = 0; $i <= 10; ++ $i) {
// //设置直线颜色
// $color = imageColorAllocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
// //在$img图像上随机画一条直线
// imageline ( $img, mt_rand ( 0, $this->img_w ), mt_rand ( 0, $this->img_h ), mt_rand ( 0, $this->img_w ), mt_rand ( 0, $this->img_h ), $color );
// }
//加加框
$rect_color = imagecolorallocate ( $img, 0x90, 0x90, 0x90 );
imagerectangle ( $img, 0, 0, $this->img_w - 1, $this->img_h - 1, $rect_color );
$str_color = imagecolorallocate ( $img, mt_rand ( 0, 100 ), mt_rand ( 0, 100 ), mt_rand ( 0, 100 ) );
$font_w = imagefontwidth ( $this->font );
$font_h = imagefontheight ( $this->font );
$str_len = $font_w * $this->char_len;
imagestring ( $img, $this->font, ($this->img_w - $str_len) / 2, ($this->img_h - $font_h) / 2, $code, $str_color );
设定字符串颜色
$str_color = imageColorAllocate ( $img, mt_rand ( 0, 100 ), mt_rand ( 0, 100 ), mt_rand ( 0, 100 ) );
//设定字符串位置
$font_w = imageFontWidth ( $this->font ); //字体宽
$font_h = imageFontHeight ( $this->font ); //字体高
$str_w = $font_w * $this->char_len; //字符串宽
imageString ( $img, $this->font, ($this->img_w - $str_w) / 2, ($this->img_h - $font_h) / 2, $code, $str_color );
echo 'ddd';
ob_clean (); //清除输出缓存
//添加到
header ( 'Content-Type: image/png' ); //header前不能加任何输出或加ob_clean()清除
imagepng ( $img );
//----4 销毁画布
imagedestroy ( $img );
return $code;
}
}
二、调用类输出验证码并将验证码保存到COOKIE:testCode.php
<?php
require 'clscode.class.php';
$code=new clsCode();
$captcha=$code->output();
setcookie('captcha', $captcha, time() + 3600) ;
三、创建testClscode.html进行验证码输入与验证
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<script>
function recode(){
change=document.getElementById('change');
change.src='testCode.php';
captcha=document.getElementById('captcha');
captcha.src=change.src;
}
function check(){
var code1;
code=document.getElementById('code').value;
code0=document.cookie.split('=')[1];//可以通过alert() 查看cookie中验证码格式
code0= code0.split(';')[0]
// alert(code.toLowerCase());
// alert(code0.toLowerCase());
if(code0.toLowerCase()==code.toLowerCase()){
alert("验证码正确");
}else{
alert("验证码不正确");
}
}
</script>
<body>
<input type='text' name='code' id='code'>
<input type="image" src='testCode.php' id='change' οnclick='recode()'>看不清,请点击图片
<a href='javascript:recode();'><img alt="看不清,点击图片" src="testCode.php" id='captcha'></a><br>
<input type='button' id='check' οnclick='check()' value='验证'>
</body>
</html>
四、测试