一个好用的PHP验证码类
参考了网上的php 生成验证码的方法,以及php 图片验证码和php 中文验证码的生成方法。用到了PHP GD库的相关知识。
1,生成验证码的类 VerificationCode.class.php
class VerificationCode{
private $charset="abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=110; //宽度
private $height=30; //高度
private $img; //图像资源句柄
private $font; //制定字体
private $fontSize=25; //字体大小
private $fontColor; //字体颜色
public function __construct(){
$this->font="CALIBRIZ.TTF";
}
//生成验证码
private function createCode(){
$len=strlen($this->charset)-1;
for ($i = 0; $i < $this->codelen; $i++) {
$this->code .= $this->charset[mt_rand(0,$len)];
}
}
//生成背景
private function createBg(){
$this->img=imagecreatetruecolor($this->width,$this->height);
$color = imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
private function createFont(){
$x=$this->width/$this->codelen;
for ($i = 0; $i < $this->codelen; $i++) {
$this->fontColor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontSize,mt_rand(-30,30),$i*$x+mt_rand(1,5),$this->height/1.4,$this->fontColor,$this->font,$this->code[$i]); // www.jbxue.com
//imagestring($this->img,5,$i*$x+mt_rand(1,5),5,$this->code[$i],$this->fontColor);
}
}
//生成线条、雪花
private function createDisturb(){
for ($i = 0; $i < 6; $i++) {
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->width),mt_rand(0,$this->width),mt_rand(0,$this->width),$color);
}
for ($i = 0; $i < 100; $i++) {
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
private function outPut(){
header("Content-Type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}
public function showCode(){
$this->createBg();
$this->createCode();
$this->createDisturb();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode(){
return strtolower($this->code);
}
}
?>
session_start();
require_once 'VerificationCode.class.php';
$code=new VerificationCode();
$_SESSION['code']=$code->getCode();
$code->showCode();
?>
验证码:<input type="text" name="code" /><img src="code.php" οnclick="javascript:this.src='code.php?time='+Math.random();" />
转载于:https://www.cnblogs.com/linuxnotes/p/3493437.html
一个好用的PHP验证码类相关推荐
- php面向对象(OOP)---- 验证码类
验证码是众多网站登陆.注册等相关功能不可以或缺的功能,实现展示验证码的方式有很多,这篇文章作者以工作中比较常用的方法进行了封装. 逻辑准备 要实现一个完整的验证码,需要包含以下属性和功能 验证码类 { ...
- 登陆cf出现1.php,cf兄弟礼包验证码 一个PHP验证码类代码分享已封装成类
代码如下:<?php session_start(): Header("Content-type: image/gif"): class SecurityCode { pri ...
- 验证码识别服务器,一个非常好用的验证码识别工具类api接口
一个非常好用的验证码识别工具类api接口 群发?批量操作?验证码?可能乖孩子对于这些单个有了解,但是对于合在一起就不知道其存在的意义.这个对 于我们日常的生活可能是没有什么用处的,但是对于需要批量检测 ...
- 分享一个漂亮的php验证码类
直接上代码: //验证码类 class ValidateCode {private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ2345 ...
- PHP网站修改简体字,修改了一个很不错的php验证码(支持中文)
很早前在PHP喜悦国际村看到的一个验证码类,写的很强.但有些逻辑方面感觉处理的不好,然后我不喜欢完全用session来处理验证码.这在大型系统中是很不好的 php英文验证码captcha.class. ...
- php验证码的封装,PHP实现的封装验证码类详解
用PHP写一个验证码类,并进行封装. 类名: validationcode.class.php代码如下: class ValidationCode { private $width; private ...
- php clicaptcha,php实现的Captcha验证码类实例
这篇文章主要介绍了php实现的Captcha验证码类,实例展示了一个验证码类程序并附有用法演示实例,有着非常好的参考借鉴价值,需要的朋友可以参考下 本文实例讲述了php实现的Captcha验证码类,在 ...
- php验证码类(分享)
//验证码类 class ValidateCode {private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/ ...
- Python中的元编程:一个关于修饰器和元类的简单教程
作者 | Saurabh Kukade 译者 | 刘畅 出品 | AI科技大本营(ID:rgznai100) 最近,作者遇到一个非常有趣的概念,它就是用 Python 进行元编程.我想在本文中分享我对 ...
最新文章
- 华为机考HJ1字符串最后一个单词的长度
- anaconda中的python如何进行关联分析_浅析python,PyCharm,Anaconda三者之间的关系
- 献礼724运维日 | 首届腾讯运维技术开放日讲了啥? (附演讲PPT领取方式)
- git 拉取远程分之到本地
- 新闻发布项目——业务逻辑层(categoryTBService)
- AUTOSAR从入门到精通100讲(二)-SOME/IP及其应用
- c语言自定义函数案例情景,第4周 C语言及程序设计提高例程-3 体验自定义函数...
- centos 和 radhat 配置epel仓库
- sql学生选课管理系统
- SOLIDWORDS API修改零部件属性全部保存
- python提取文件指定列_python 提取文件指定列的方法示例
- WIN7系统做NTP服务器,大师教您win7系统搭建ntp服务器的图文技巧
- 软件全屏使用时点击鼠标自动跳回桌面的问题
- STM32单片机的学习
- Remove 以及dorp做实验验证MongoDB删除文档后索引是否会自动删除
- Python爬取某站上海租房图片!
- 用Python做一个简陋的文本编辑器
- ICMP报文格式详解
- 海伦司上市在即:“嚣张”气势堪比瑞幸,核心竞争力存不存在?
- 用python写跑酷游戏脚本,用Python写一个天天酷跑