本文目录一览:
如何用PHP生成验证码
PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:
1、产生一张png的图片;
2、为图片设置背景色;
3、设置字体颜色和样式;
4、产生4位数的随机的验证码;
5、把产生的每个字符调整旋转角度和位置画到png图片上;
6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;
7、输出图片;
8、释放图片所占内存。
session_start();
getCode(4,60,20);
function getCode($num,$w,$h) {
$code = "";
for ($i = 0; $i $num; $i++) {
$code .= rand(0, 9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证时用
$_SESSION["helloweba_num"] = $code;
//创建图片,定义颜色值
header("Content-type: image/PNG");
$im = imagecreate($w, $h);
$black = imagecolorallocate($im, 0, 0, 0);
$gray = imagecolorallocate($im, 200, 200, 200);
$bgcolor = imagecolorallocate($im, 255, 255, 255);
//填充背景
imagefill($im, 0, 0, $gray);
//画边框
imagerectangle($im, 0, 0, $w-1, $h-1, $black);
//随机绘制两条虚线,起干扰作用
$style = array ($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im, $style);
$y1 = rand(0, $h);
$y2 = rand(0, $h);
$y3 = rand(0, $h);
$y4 = rand(0, $h);
imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用;
for ($i = 0; $i 80; $i++) {
imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
}
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx = rand(3, 8);
for ($i = 0; $i $num; $i++) {
$strpos = rand(1, 6);
imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
$strx += rand(8, 12);
}
imagepng($im);//输出图片
imagedestroy($im);//释放图片所占内存
}
php 调用验证码为一个类 怎么调用
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;$posstrlen($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/abr';
else echo'a style="color:red"Your Entered Cod Was Incorrect/abr';
}
?
img src="captcha.php?cap=login.png"
form action="?php echo $_SERVER['PHP_SELF']; //safe it later (xss)?" method="post"
aINPUT TEXT :/abr
input type="text" name="captchacod"br
input type="submit" value="check"br
/form
请问PHP生成验证码的类怎么写?
?php
class Code{
// 1. 定义各个成员 有宽、高、画布、字数、类型、画类型
private $width; //宽度
private $height; //高度
private $num; //验证码字数
private $imgType; //生成图片类型
private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合
private $hb; //画布
public $codestr; // 验证码字串
public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){
$this-width = $num*20;
$this-height = $height;
$this-num = $num;
$this-imgType = $imgType;
$this-Type = $Type;
$this-codestr = $this-codestr();
$this-zuhe();
}
// 2. 定义随机获取字符串函数
private function codestr(){
switch($this-Type){
case 1: // 类型为1 获取1-9随机数
$str = implode("",array_rand(range(0,9),$this-num));
break;
case 2: // 类型为2 获取a-z随机小写字母
$str = implode("",array_rand(array_flip(range(a,z)),$this-num));
break;
case 3: // 类型为3 获取数字,小写字母,大写字母 混合
for($i=0;$i$this-num;$i++){
$m = rand(0,2);
switch($m){
case 0:
$o = rand(48,57);
break;
case 1:
$o = rand(65,90);
break;
case 2:
$o = rand(97,122);
break;
}
$str .= sprintf("%c",$o);
}
break;
}
return $str;
}
// 3. 初始化画布图像资源
private function Hb(){
$this-hb = imagecreatetruecolor($this-width,$this-height);
}
// 4. 生成背景颜色
private function Bg(){
return imagecolorallocate($this-hb,rand(130,250),rand(130,250),rand(130,250));
}
// 5. 生成字体颜色
private function Font(){
return imagecolorallocate($this-hb,rand(0,100),rand(0,100),rand(0,100));
}
// 6. 填充背景颜色
private function BgColor(){
imagefilledrectangle($this-hb,0,0,$this-width,$this-height,$this-Bg());
}
// 7. 干扰点
private function ganrao(){
$sum=floor(($this-width)*($this-height)/3);
for($i=0;$i$sum;$i++){
imagesetpixel($this-hb,rand(0,$this-width),rand(0,$this-height),$this-Bg());
}
}
// 8. 随机直线 弧线
private function huxian(){
for($i=0;$i$this-num;$i++){
imageArc($this-hb,rand(0,$this-width),rand(0,$this-height),rand(0,$this-width),rand(0,$this-height),rand(0,360),rand(0,360),$this-Bg());
}
}
// 9. 写字
private function xiezi(){
for($i=0;$i$this-num;$i++){
$x=ceil($this-width/$this-num)*$i;
$y=rand(1,$this-height-15);
imagechar($this-hb,5,$x+4,$y,$this-codestr[$i],$this-Font());
}
}
// 10. 输出
private function OutImg(){
$shuchu="image".$this-imgType;
$header="Content-type:image/".$this-imgType;
if(function_exists($shuchu)){
header($header);
$shuchu($this-hb);
}else{
exit("GD库没有此类图像");
}
}
// 11. 拼装
private function zuhe(){
$this-Hb();
$this-BgColor();
$this-ganrao();
$this-huxian();
$this-xiezi();
$this-OutImg();
}
public function getCodeStr(){
return $this-codestr;
}
}
$a = new Code();
$a -getCodeStr();
?