|
要想用php编写验证码,首先要看你的gd库是否开启了,如果开启了,那么下面就位大家提供下简单的三个文件,这三个问价结合在一起就能实现验证码了:
login.php
<?php
session_start();
$a=@strtoupper($_GET["input1"]);
$b=@strtoupper($_SESSION["code"]);
if($a==$b){
echo "验证码输入正确";
}else{
echo "验证码有误";
}
?>
<form action="" method="get" name="form1">
<input type="text" name="input1" onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()"> <img src="code.php" > <font size="3">看不清点击图片更换</font> <hr>
<input type="submit" value="提交">
</form>
check_code.php
<?php
class CheckCode {
private $image;
private $width;
private $height;
private $codeNum;
private $disturbNum;
private $checkCode;
function __construct($width=100,$height=30,$codeNum=4){
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
$this->checkCode=$this->createCheckCode();
$number = floor($width*$height/15);
if($number > 240-$codeNum){
$this->disturbNGum=240-$codeNum;
}else{
$this->disturbNum=$number;
}
}
//将验证码输出到浏览器上
function showImage(){
//第一步创建验证码图像
$this->createimage();
//第二步加入干扰素
$this->setdisturbcode();
//第三步向图片中随机的写入文本内容
$this->outputtext();
//输出图像
$this->outputimage();
}
//客户端保存验证码
function getcodeimage(){
return $this->checkCode;
echo $this->checkCode;
}
//制作验证码
private function createimage(){
$this->image=imagecreatetruecolor($this->width,$this->height);
//随机的变换图片的背景颜色
$bgcolor=imagecolorallocate($this->image,rand(225,255),rand(225,255),rand(225,255));
//填充背景色
imagefill($this->image,0,0,$bgcolor);
//边框的颜色
$bordercolor=imagecolorallocate($this->image,0,0,0);
//画一个矩形块
imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$bordercolor);
}
//加入干扰素
private function setdisturbcode(){
for($i=0; $i<$this->disturbNum; $i++){
$pixelcolor=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$pixelcolor);
}
for($i=0; $i<10; $i++){
$circlecolor=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
imagearc($this->image,rand(3,$this->width),rand(3,$this->height),rand(10,$this->width-5),rand(10,$this->height-5),45,180,$circlecolor);
}
}
//创建验证字符
private function createCheckCode(){
$str="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIGKMNPQRSTUVWXYZ";
$code="";
for($i=0; $i<$this->codeNum; $i++){
$code.=$str{rand(0,strlen($str)-1)};
}
return $code;
}
//插入字符
private function outputtext(){
for($i=0; $i<$this->codeNum; $i++){
$fontcolor=imagecolorallocate($this->image,rand(0,120),rand(0,120),rand(0,120));
$fontsize=rand(3,6);
$y=rand(0,$this->height-15);
$x=floor($this->width/$this->codeNum)*$i+5;
//imagettftext($this->image,$fontsize,0,$x,$y,$fontcolor,"../font/simhei.ttf",$this->checkCode{$i});
imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor);
}
}
//输出验证码
private function outputimage(){
if(imagetypes() & IMG_PNG){
header("Content-type:image/png");
imagepng($this->image);
} else if(imagetypes($img) & IMG_JPEG){
header("Content-type:image/jpeg");
imagejpeg($this->image);
} else if(imagetypes($img) & IMG_GIF){
header("Content-type:image/gif");
imagegif($this->image);
} else{
echo "<script> alert('php不支持此类型的图片'); </script>";
}
}
}
?>
code.php
<?php
session_start();
include("check_code.php");
$code=new CheckCode(100,30,4);
$code->showImage();
$_SESSION["code"]=$code->getcodeimage();
?> |
|