首页 > 编程知识 正文

php识别验证码的库(php自动识别验证码)

时间:2023-12-05 19:35:23 阅读:312406 作者:JKNW

本文目录一览:

  • 1、如何用PHP生成验证码
  • 2、php 短信验证码数据库如何设计
  • 3、php图片验证码实现
  • 4、PHP图形验证码识别
  • 5、php 识别验证码

如何用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 短信验证码数据库如何设计

php做短信验证码,需要将手机号,发送的验证码和时间这几个存到数据库,在添加到数据库的时候,要判断里面有没有要存的手机号,有的话,就更新验证码和时间,没有就是添加,在使用验证码判定的时候,取出验证码和时间,判断验证码是否正确,时间是否在自己设置的有效时间段内,整个过程就是这样。

php图片验证码实现

可以用php的GD库做

//随机生成验证码

class randomString

{

function createRandomStr($strLen)

{

list($usec, $sec) = explode(' ', microtime());

(float) $sec + ((float) $usec * 100000);

$number = '';

$number_len = $strLen;

$stuff = '1234567890abcdefghijklmnopqrstuvwxyz';//附加码显示范围ABCDEFGHIJKLMNOPQRSTUVWXYZ

$stuff_len = strlen($stuff) - 1;

for ($i = 0; $i $number_len; $i++) {

$number .= substr($stuff, mt_rand(0, $stuff_len), 1);

}

return $number;

}

}

通过ZD库将验证码变成图片

$number = $createStr-createRandomStr('4');//验证码的位数

$number_len = strlen($number);

$_SESSION["VERIFY_CODE"] = $number;

// 生成验证码图片

$img_width = 60;

$img_height = 20;

$img = imageCreate($img_width, $img_height);

ImageColorAllocate($img, 0x6C, 0x74, 0x70);

$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);

$ix = 6;

$iy = 2;

for ($i = 0; $i $number_len; $i++) {

imageString($img, 5, $ix, $iy, $number[$i], $white);

$ix += 14;

}

for($i=0;$i200;$i++) //加入干扰象素

{

$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);

}

// 输出图片

header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));

imagepng($img);

imagedestroy($img);

PHP图形验证码识别

1.验证码取出,转到8位或者24位位图

2.位图处理,二值化,RGB设定阀值小于阀值时为1否则为0 1为黑神色,0为白色

3.位图处理,去噪点干扰,利用二值化的位图,八方向法,一个孤立的噪点周围八个方向的点都是0白色。

4.干扰线,定义边界均为白色的区域,若干像素,让该区域在位图移动,如果进入区域内的黑色点小于某值时认定为噪点或干扰线。

5.分割。简单平均分布可以直接固定分割。复杂的有粘连的利用投影,求字符数+1个极小值或极大值。极小值之间最小距离极小值到前一个极小值距离极小值之间最大距离。最大距离和最小距离按照字符长度来目测,一点一点的对比得出适当的值。

虽然此法可以解决部分粘连验证码,但是对于一些变态变形的公共区域比较多的验证码是无效的。

如果想知道更多分割方法,请到百度文库,搜索验证码分割。

6.识别。建立特征库,或者利用神经网络自动学习。

然后比对,字节或者文本均可。相似度自己设定,一般在90%以上

这些理论知识都学习明白了,基本就可以去做识别验证码了。

验证码最最重要且最难的一点就是分割。

有些方法不需要分割也可以借鉴一下。

php 识别验证码

PHP识别验证码(适合大部分验证码)

?php

$ch= curl_init();

$img='@D:APMServ5.2.6wwwhtdocsccbntef.png';//注意@,表示文件上传

$data=array(

'type'='recognize',

'softID'='3',

'softKey'='623527b90698a47ec626043dac04a0f1',

'userName'='test',

'passWord'='123456',

'imagePath'=$img,

'codeType'='1040',//验证码类型,见下面图片

'timeout'='60',

'remark'='',

'log'='0',

'upload'='开始识别'

);

// $ch =

curl_init();

//

curl_setopt($ch, CURLOPT_URL, $url);

//

curl_setopt($ch, CURLOPT_POST, 1);

//

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

//

curl_exec($ch);

//

curl_close($ch);

//

这段代码提交出去的Content-Type到底是multipart/form-data还是application/x-www-form-urlencoded呢?我抓包研究了一下,发现Content-Type的类型取决于$data的数据类型。

//

如果$data是字符串,则Content-Type是application/x-www-form-urlencoded。

//

如果$data是k=v的数组,则Content-Type是multipart/form-data

$url='';

curl_setopt($ch, CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

$result=

curl_exec($ch);

echo$result;

curl_close($ch);

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