首页 > 编程知识 正文

php的验证码类,PHP生成验证码

时间:2023-12-28 11:56:45 阅读:327679 作者:IPNU

本文目录一览:

请问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();

?

php如何实现验证码?许昌鲤鱼IT计算机电脑软件编程培训中心

验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。好吧,其实是没有事情干,但是又不想浪费时间,所以学习了下php实现验证码。正所谓,技多不压身。而且,也可以封装成一个函数,以后使用的时候也是很方便的,当然现在未封装。

现在来说说简单的纯数字验证码吧。

如果是初学者,建议按照我代码的注释 //数字 一步步来。最简单的方法,还是把整个代码复制走了。

新建一个captcha.php:

php //10设置session,必须处于脚本最顶部

session_start(); $image = imagecreatetruecolor(100, 30); //1设置验证码图片大小的函数

//5设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色

imagefill($image, 0, 0, $bgcolor); //10设置变量

$captcha_code = ""; //7生成随机数字

for($i=0;$i4;$i++){ //设置字体大小

$fontsize = 6;

//设置字体颜色,随机颜色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色

//设置数字

$fontcontent = rand(0,9); //10.=连续定义变量

$captcha_code .= $fontcontent;

//设置坐标

$x = ($i*100/4)+rand(5,10); $y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

} //10存到session

$_SESSION['authcode'] = $captcha_code; //8增加干扰元素,设置雪花点

for($i=0;$i200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 画一个单一像素

imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

} //9增加干扰元素,设置横线

for($i=0;$i4;$i++){ //设置线的颜色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线

imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);

} //2设置头部,image/png

header('Content-Type: image/png'); //3imagepng() 建立png图形函数

imagepng($image); //4imagedestroy() 结束图形函数 销毁$image

imagedestroy($image);

接着就是静态页的代码了:index.html

doctype htmlhtml

head

meta http-equiv="Content-Type" content="text/html; charset=UTF-8"

title确认验证码title

head

body

form method="post" action="./form.php"

p验证码: img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?' style="width:100px; height:30px" / a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()"换一个?a

p

P请输入验证码:input type="text" name='authcode' value=''/p

pinput type='submit' value='提交' style='padding:6px 5px;'/p

bodyhtml

从index.html可以看到,提交的表单是到form.php的,所以还要有一个判断的form.php代码:

php header("Content-Type:text/html;charset=utf-8"); //设置头部信息

//isset()检测变量是否设置

if(isset($_REQUEST['authcode'])){ session_start(); //strtolower()小写函数

if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){ //跳转页面

echo "script language="javascript""; echo "document.location="./form.php""; echo "/script";

}else{ //提示以及跳转页面

echo "script language="javascript""; echo "alert('输入错误!');"; echo "document.location="./form.php""; echo "/script";

} exit();

}

那么,纯数字的实现了,数字加英文的也应该不难了。要修改的代码 只是在 captcha.php 将 //7生成随机数字 修改成 //7生成随机的字母和数字,如果你真的很可爱的就修改这几个字就认为可以实现的话,那么祝贺你,你永远保持快乐。脑残儿童欢乐多。

废话不多说了,拉代码吧。

php //10设置session,必须处于脚本最顶部

session_start(); $image = imagecreatetruecolor(100, 30); //1设置验证码图片大小的函数

//5设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色

imagefill($image, 0, 0, $bgcolor); //10设置变量

$captcha_code = ""; //7生成随机的字母和数字

for($i=0;$i4;$i++){ //设置字体大小

$fontsize = 8;

//设置字体颜色,随机颜色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色

//设置需要随机取的值,去掉容易出错的值如0和o

$data ='abcdefghigkmnpqrstuvwxy3456789'; //取出值,字符串截取方法 strlen获取字符串长度

$fontcontent = substr($data, rand(0,strlen($data)),1); //10.=连续定义变量

$captcha_code .= $fontcontent;

//设置坐标

$x = ($i*100/4)+rand(5,10); $y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

} //10存到session

$_SESSION['authcode'] = $captcha_code; //8增加干扰元素,设置雪花点

for($i=0;$i200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 画一个单一像素

imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

} //9增加干扰元素,设置横线

for($i=0;$i4;$i++){ //设置线的颜色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线

imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);

} //2设置头部,image/png

header('Content-Type: image/png'); //3imagepng() 建立png图形函数

imagepng($image); //4imagedestroy() 结束图形函数 销毁$image

imagedestroy($image);

其他的两个页面,不许要修改。

一般而言,现在就已经够用了。但是就像动漫一样,总会有番外。

那么,我们来个汉字的番外吧。其实我也准备将汉字的验证码放到我的毕业设计里面,虽然现在很流行滑动验证码,但是本人毕竟不是专门学习js的。

而且,还可以和答辩的老师说,我们验证码不需要素材,连图片也是生成的,用自己的知识装13,也没有设么的。

php //11设置session,必须处于脚本最顶部

session_start(); //1设置验证码图片大小的函数

$image = imagecreatetruecolor(200, 60);

//5设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色

imagefill($image, 0, 0, $bgcolor); //7设置ttf字体

$fontface = 'FZYTK.TTF'; //7设置字库,实现简单的数字储备

$str='天地不仁以万物为刍狗圣人不仁以百姓为刍狗这句经常出现在控诉暴君暴政上地残暴不仁把万物都当成低贱的猪狗来看待而那些高高在上的所谓圣人们也没两样还不是把我们老百姓也当成猪狗不如的东西但实在正取的解读是地不情感用事对万物一视同仁圣人不情感用事对百姓一视同仁执子之手与子偕老当男女主人公含情脉脉看着对方说了句执子之手与子偕老女方泪眼朦胧含羞地回一句讨厌啦这样的情节我们是不是见过很多但是我们来看看这句的原句死生契阔与子成说执子之手与子偕老于嗟阔兮不我活兮于嗟洵兮不我信兮意思是说战士之间的约定说要一起死现在和我约定的人都走了我怎么活啊赤裸裸的兄弟江湖战友友谊啊形容好基友的基情比男女之间的爱情要合适很多吧'; //str_split()切割字符串为一个数组,一个中文在utf_8为3个字符

$strdb = str_split($str,3);

//11

$captcha_code = ''; //8生成随机的汉子

for($i=0;$i4;$i++){ //设置字体颜色,随机颜色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色

//随机选取中文

$in = rand(0,count($strdb)); $cn = $strdb[$in]; //将中文记录到将保存到session的字符串中

$captcha_code .= $cn; /*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,

string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐标,颜色,字体路径,文本字符串

mt_rand()生成更好的随机数,比rand()快四倍*/

imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);

} //11存到session

$_SESSION['authcode'] = $captcha_code; //9增加干扰元素,设置点

for($i=0;$i200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 画一个单一像素

imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);

} //10增加干扰元素,设置线

for($i=0;$i4;$i++){ //设置线的颜色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线

imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);

} //2设置头部,image/png

header('Content-Type: image/png'); //3imagepng() 建立png图形函数

imagepng($image); //4imagedestroy() 结束图形函数 销毁$image

imagedestroy($image);

其他的页面也是不需要修改的。

效果图如下:

php怎么实现验证码的

验证码功能机制实现思路

常规的验证码实现:

a、产生一张png的图片

b、为图片设置背景色

c、设置字体颜色和样式

d、产生4位数的随机的验证码

e、把产生的每个字符调整旋转角度和位置画到png图片上

f、加入噪点和干扰线防止注册机器分析原图片来恶意注册

g、输出图片

h、释放图片所占内存

i、将验证码保存到session或是数据库

j、将和输入的验证码进行对比

短信(邮箱)验证码机制:

a、产生4-6位数的随机的验证码

b、把产生的每个字符保存到session或是数据库

c、将验证码发送到用户的手机(邮箱)

d、用户在规定时间内进行输入

e、将验证码从session或是数据库中取出

f、将和输入的验证码进行对比验证

php验证码类帮看下,谢谢很短

一、修改php.ini(保举)

memory_limit = 12M

2、在程序里面添加如下语句

ini_set(''memory_limit'', ''12M'');

3、在根目录建立

.htaccess文件,添加如下内容

php_value memory_limit 12M

如果还不能解决,就把它改得再大一些。

最后注意的是重启服务器

==========================

上面是我抄的,看到你的提问我感觉是配置的问题,但我这人记不住东西,只能找,其实你把报错的提示一搜就会有答案的

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生成验证码的原理:使用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);//释放图片所占内存 

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。