1,php验证码类
<?php
// usage:
/*
显示验证码:
<img src="captcha.php?cap=login.png">
检查验证码:
检查输入的验证码与 $_SESSION['login'] 中保存的值是否相等。
*/
error_reporting(E_ALL);
session_start();
(!isset($_GET['cap']))?die('Error !'):1;
$captcha_array=array('login.png','contact.png','comment.png');
(!in_array($_GET['cap'],$captcha_array))?die('Error !'):1;
$captcha_cod=new captcha(basename($_GET['cap'],'.png')) ;
//验证码类
class captcha
{
private $session_name;
private $image_width;
private $image_height;
private $cod_length;
private $cod_mode;
private $font_path;
private $avtage_font_size;
private $sec_cod;
private $res_image;
function __construct($name,$width=200,$height=50,$length=5,$mod=2,$font='arial.ttf',$av_font_size=25)
{
$this-> session_name = $name ;
$this-> image_width = $width ;
$this-> image_height = $height ;
$this-> cod_length = $length ;
$this-> mode = $mod ;
$this-> font_path = $font ;
$this-> avrage_font_size = $av_font_size ;
$this->Gen_Cod();
}
function Write_Text($text)
{
$x_pos=10;
for($pos=0;$pos<strlen($text);$pos++) {
imagettftext($this->res_image,rand($this->avrage_font_size -2,$this->avrage_font_size +2),
rand(-40,+40),$x_pos,rand(35,$this->image_height - $this->avrage_font_size),
imagecolorallocate($this->res_image,rand(0,150),rand(0,150),rand(0,150)),
$this->font_path,$text[$pos]);
$x_pos+=($this->image_width/$this->cod_length);
}
}
function Draw_Line()
{
//
for($pos=0;$pos<$this->image_height;$pos+=8)
imageline($this->res_image,0,$pos,$this->image_width,$pos,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230)));
//
for($pos=0;$pos<$this->image_width;$pos+=8)
imageline($this->res_image,$pos,0,$pos,$this->image_height,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230)));
}
function Gen_Cod()
{
//generate rand cod :
//mode:1 => 0-9 , mode:2 => 0-9 , a-z
($this->mode==1) ? $this->sec_cod=substr((string)rand(1000000000,9999999999),0,$this->cod_length) :
$this->sec_cod=substr(md5(rand(1000000000,9999999999)),0,$this->cod_length);
//set session :
$_SESSION[$this->session_name] = $this->sec_cod ;
//creat image :
$this->res_image=imagecreatetruecolor( $this->image_width , $this->image_height );
//fill color:
imagefilledrectangle($this->res_image,0,0,$this->image_width,$this->image_height,imagecolorallocate($this->res_image,255,255,255));
//write text :
$this->Write_Text($this->sec_cod);
//draw line :
$this->Draw_Line();
//output :
imagejpeg($this->res_image);
header('content-type:image/jpeg');
//destroy:
imagedestroy($this->res_image);
}
}
2,php验证码类的调用示例:
<?php
session_start();
if(isset($_POST['captchacod'])){
if($_SESSION['login']==$_POST['captchacod'])echo'<a style="color:green">Your Entered Cod Was Correct</a><br>';
else echo'<a style="color:red">Your Entered Cod Was Incorrect</a><br>';
}
?>
<img src="captcha.php?cap=login.png" >
<form action="<?php echo $_SERVER['PHP_SELF']; //safe it later (xss)?>" method="post">
<a>INPUT TEXT :</a><br>
<input type="text" name="captchacod"><br>
<input type="submit" value="check"><br>
</form>
您可能感兴趣的文章:
php验证码简单函数代码(附效果图)
php验证码的三个实例代码分享
一个php验证码的封装类
php自定义大小验证码的实例代码
php生成扭曲及旋转的验证码图片的实例代码
php仿QQ验证码的实现代码
php验证码函数使用的例子
php5验证码类(简易实用型)
php验证码(GD库生成验证码)的例子
php点击验证码实时刷新的实现代码
php图片验证码的例子
php彩色验证码的简单例子
php验证码刷新与局部刷新的实现方法
php GD库生成验证码的实例
php生成验证码的例子
php随机验证码 php生成随机验证码(图文)
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码