首页 / PHP / 网络验证码自动生成程序
网络验证码自动生成程序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了网络验证码自动生成程序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6308字,纯文字阅读大概需要10分钟。
内容图文
![网络验证码自动生成程序](/upload/InfoBanner/zyjiaocheng/198/793f66a81d484ab8b4122d761a01aa14.jpg)
回复讨论(解决方案)
直接用google的吧。
https://developers.google.com/recaptcha/
网上多得是,搜索一下就是了。
直接用google的吧。
https://developers.google.com/recaptcha/
是不是调用google的api就可以了?
是啊,调用google的api,这个安全系数还是相当高的。
<?php
class captcha {
//
private $captcha_key = 'captcha_code';//session内的元素的下标
private $width;//图片的宽
private $height;//图片的高
/**
*构造方法
* @param $width integer 宽
* @param $height integer 高
*/
public function __construct() {
}
public function generate($width, $height) {
//初始化属性
$this->width = $width;
$this->height = $height;
//制作验证码
$chars = array_merge(range('a', 'z'), range('A', 'Z'), range('1','9'));
$rand_keys = array_rand($chars, 4);
shuffle($rand_keys);
foreach($rand_keys as $key) {
$char[] = $chars[$key];
}
//放入到session
@session_start();
//将字符串连接起来,保存到session内
//implode(), 如果不写第一个参数,则表示空字符连接。
$_SESSION[$this->captcha_key] = implode($char);
//处理图片
//step1
$img = imagecreatetruecolor($this->width, $this->height);
//step2
//填充背景
// 先设计颜色,分配到图片上
$backgroud_color = imagecolorallocate($img, mt_rand(180, 255), mt_rand(180, 255), mt_rand(180, 255));
//填充到图片上
imagefill($img, 0, 0, $backgroud_color);
//增加一些干扰线段
//先分配线段颜色
for($i=0; $i<5; $i++) {
$line_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
//画线
imageline($img, mt_rand(0, $this->width-1), mt_rand(0, $this->height-1), mt_rand(0, $this->width-1), mt_rand(0, $this->height-1), $line_color);
}
//增加干扰像素
for($i = 0; $i<100; $i++) {
$pixel_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
imagesetpixel($img, mt_rand(0, $this->width-1), mt_rand(0, $this->height-1), $pixel_color);
}
//先获得可以在资源上使用的颜色
$string_color = imagecolorallocate($img, mt_rand(80, 150), mt_rand(80, 150), mt_rand(80, 150));//
//将获得的字符串 写到图片上
imagestring($img, 5, 55, 2, $_SESSION[$this->captcha_key], $string_color);
//step3
//设置回应头,我们要输出到浏览器的内容是 png格式的图片
header('Content-Type:image/png');
imagepng($img);//如果没有第二个参数,就表示是输出的意思。
//step 4
imagedestroy($img);
}
/**
* 判断用户提交的验证码是否正确
* @param $code string 用户提交的数据
* @return bool 一致则返回true 不一致返回false
*/
public function checkCaptcha($code) {
return strtoupper($code) == strtoupper($_SESSION[$this->captcha_key]);
}
}
具体怎么用,你可以网上下载看看韩顺平的视频教程 第120讲 session④-验证码防恶意攻击。以上代码是从其讲课笔记中down下来的。
<?php
class captcha {
//
private $captcha_key = 'captcha_code';//session内的元素的下标
private $width;//图片的宽
private $height;//图片的高
/**
*构造方法
* @param $width integer 宽
* @param $height integer 高
*/
public function __construct() {
}
public function generate($width, $height) {
//初始化属性
$this->width = $width;
$this->height = $height;
//制作验证码
$chars = array_merge(range('a', 'z'), range('A', 'Z'), range('1','9'));
$rand_keys = array_rand($chars, 4);
shuffle($rand_keys);
foreach($rand_keys as $key) {
$char[] = $chars[$key];
}
//放入到session
@session_start();
//将字符串连接起来,保存到session内
//implode(), 如果不写第一个参数,则表示空字符连接。
$_SESSION[$this->captcha_key] = implode($char);
//处理图片
//step1
$img = imagecreatetruecolor($this->width, $this->height);
//step2
//填充背景
// 先设计颜色,分配到图片上
$backgroud_color = imagecolorallocate($img, mt_rand(180, 255), mt_rand(180, 255), mt_rand(180, 255));
//填充到图片上
imagefill($img, 0, 0, $backgroud_color);
//增加一些干扰线段
//先分配线段颜色
for($i=0; $i<5; $i++) {
$line_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
//画线
imageline($img, mt_rand(0, $this->width-1), mt_rand(0, $this->height-1), mt_rand(0, $this->width-1), mt_rand(0, $this->height-1), $line_color);
}
//增加干扰像素
for($i = 0; $i<100; $i++) {
$pixel_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
imagesetpixel($img, mt_rand(0, $this->width-1), mt_rand(0, $this->height-1), $pixel_color);
}
//先获得可以在资源上使用的颜色
$string_color = imagecolorallocate($img, mt_rand(80, 150), mt_rand(80, 150), mt_rand(80, 150));//
//将获得的字符串 写到图片上
imagestring($img, 5, 55, 2, $_SESSION[$this->captcha_key], $string_color);
//step3
//设置回应头,我们要输出到浏览器的内容是 png格式的图片
header('Content-Type:image/png');
imagepng($img);//如果没有第二个参数,就表示是输出的意思。
//step 4
imagedestroy($img);
}
/**
* 判断用户提交的验证码是否正确
* @param $code string 用户提交的数据
* @return bool 一致则返回true 不一致返回false
*/
public function checkCaptcha($code) {
return strtoupper($code) == strtoupper($_SESSION[$this->captcha_key]);
}
}
具体怎么用,你可以网上下载看看韩顺平的视频教程 第120讲 session④-验证码防恶意攻击。以上代码是从其讲课笔记中down下来的。
问下:如果想生成google类似的代码,该怎么做?
内容总结
以上是互联网集市为您收集整理的网络验证码自动生成程序全部内容,希望文章能够帮你解决网络验证码自动生成程序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。