一个漂亮的php验证码类(分享)
直接上代码:
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
private $code;//验证码
private $codelen = 4;//验证码长度
private $width = 130;//宽度
private $height = 50;//高度
private $img;//图形资源句柄
private $font;//指定的字体
private $fontsize = 20;//指定字体大小
private $fontcolor;//指定字体颜色
//构造方法初始化
public function __construct() {
$this->font = dirname(__FILE__).'/font/elephant.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),$_x*$i+mt_rand(1,5),$this->height
/ 1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成线条、雪花
private function createLine() {
//线条
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->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$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 doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode() {
return strtolower($this->code);
}
}
输出实例:
使用方法:
1、先把验证码类保存为一个名为 ValidateCode.class.php 的文件;
2、新建一个名为 captcha.php 的文件进行调用该类;
captcha.php
require './ValidateCode.class.php'; //先把类包含进来,实际路径根据实际情况进行修改。
$_vc = new ValidateCode(); //实例化一个对象
$_vc->doimg();
$_SESSION['authnum_session'] = $_vc->getCode();//验证码保存到SESSION中
3、引用到页面中,代码如下:
4、一个完整的验证页面,代码如下:
session_start();
//在页首先要开启session,
//error_reporting(2047);
session_destroy();
//将session去掉,以每次都能取新的session值;
//用seesion 效果不错,也很方便
?>
<html>
<head>
<title>session 图片验证实例</title>
<style type="text/css">
#login p{
margin-top: 15px;
line-height: 20px;
font-size: 14px;
font-weight: bold;
}
#login img{
cursor:pointer;
}
form{
margin-left:20px;
}
</style>
</head>
<body>
<form id="login" action="" method="post">
<p>此例为session验证实例</p>
<p>
<span>验证码:</span>
<input type="text" name="validate" value="" size=10>
<img title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
</p>
<p>
<input type="submit">
</p>
</form>
<?php
//打印上一个session;
//echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";
$validate="";
if(isset($_POST["validate"])){
$validate=$_POST["validate"];
echo "您刚才输入的是:".$_POST["validate"]."<br>状态:";
if($validate!=$_SESSION["authnum_session"]){
//判断session值与用户输入的验证码是否一致;
echo "<font color=red>输入有误</font>";
}else{
echo "<font color=green>通过验证</font>";
}
}
?>
一个漂亮的php验证码类(分享)的更多相关文章
- 一个漂亮的php验证码类
一个漂亮的php验证码类(分享) 作者: 字体:[增加 减小] 类型:转载 下面小编就为大家分享一个漂亮的php验证码类.需要的朋友可以过来参考下 直接上代码: 复制代码 代码如下: //验证 ...
- 一个经典的PHP验证码类分享
我们通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现 的细节封装在该类中.只要在创建对象时,为构造方法提供三 ...
- 建立一个漂亮的PHP验证码类文件及调用方式
//验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/ ...
- PHP入门培训教程 一个漂亮的PHP验证码
如何写一个漂亮的PHP验证码?兄弟连PHP培训 小编分享一段代码给大家: <?php class Imagecode{ private $width ; private $height; pri ...
- [验证码实现] Captcha 验证码类,一个很个性的验证码类 (转载)
点击下载 Captcha.zip /// <summary> /// 类说明:条码生成类 /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=htt ...
- 一个漂亮的PHP验证码
自己导入字体,可以按照自己的额需要随便修改. <?php class Imagecode{ private $width ; private $height; private $counts; ...
- ThinkPHP验证码类的使用
1.创建一个方法并引入验证码类class ShowAction extends Action{//用户评论验证码public function verify(){import('ORG.Util.Im ...
- PHP 简单面向对象 验证码类(静态实例对象调用)
没事写了一个简单的面向对象验证码类,可以直接使用(替换一下字体路径) <?php class authCode { private static $instance = null; #实例对象 ...
- 一个好用的PHP验证码类
分享一个好用的php验证码类,包括调用示例. 说明: 如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用到imagettftext()函数.字体的位置在C盘下 ...
随机推荐
- BZOJ 1088 扫雷Mine
今天做了几道BZOJ的题,发现统观题目时还是很多很多都不会的,不过还是有几道时可以作的,以后要慢慢加强,争取多做题 BZOJ 1088 扫雷 其实本人平常不大玩扫雷的,就算玩也不是很好,不过看n*2的 ...
- 【bzoj3884】 上帝与集合的正确用法
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 (题目链接) 题意 求 Solution 解决的关键: 当${n>φ(p)}$,有$${ ...
- wpf listview 换行
<ListView Name="listView1" VerticalAlignment="Top" Height="600" Ma ...
- CCTray配置如何添加远程服务器
前提: Windows防火墙必须开通的TCP端口 或者直接把防火墙关闭(不建议) 或者直接在防火墙规则增加CCNET的服务进去 总者,只要确保能telnet ip 21234能通即可 建议全部软件都装 ...
- 使用Github Pages创建自己的网站
这是一篇使用Github Pages创建自己网站的教程,操作很简单,相信,亲们肯定一学就会,但是大家也要有一定的github基础呀,所以小编给大家附上一个链接(http://www.cnblogs.c ...
- luars232库中用到的一些C API for lua
代码就不贴了,这里只是梳理一下前两篇里面忽略的一些东西,作为读代码的记录吧. 1.头文件 #include <lauxlib.h> #include <lua.h> All A ...
- C# 静态类与非静态类、静态成员的区别
静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 new 关键字创建静态类类型的变量.在声明一个类时使用static关键字,具有两个方面的意义:首先,它防止程序员写代码来实例 ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- xml解析模块
XML XML是可扩展标记语言的缩写,是实现不同语言或程序之间进行数据交换的协议,主要可以对key添加属性. 页面做展示(字符类型的一个xml格式数据)\做配置文件(内部xml格式的数据).,每一个节 ...
- CSS3动画(性能篇)
写在前面 高性能移动Web相较PC的场景需要考虑的因素也相对更多更复杂,我们总结为以下几点: 流量.功耗与流畅度. 在PC时代我们更多的是考虑体验上的流畅度,而在Mobile端本身丰富的场景下,需要额 ...