
正文
php数据验证类 php验证码类
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
求一个php验证码及如何使用的例子?
以前项目中的东西
在没有GB库的情况下也可以生成验证码
关键部分做出了注释
img src="image.php"
?php
//存为文件 image.php
error_reporting(E_ALL ^ E_NOTICE);
require_once('image.class.php');
$image_model = new Image();
//可以在此处更改属性达到验证码不同大小的图片
$image_model-outputBrowser();
?
?php
/*
* \验证码生成类 存为文件image.class.php
* \如果不支持GD库,则采用像素画法
* \详见
*/
class Image
{
var $img_height = 20; //图片的长
var $img_width = 68; //图片的宽
var $img_cache = ''; //图像信息缓存
var $check_number = ''; //验证码
var $lenght = 4; //验证码位数
var $is_gd = false;
//'----------以下为不支持GD需要的变量
var $pixels = '';
var $digits = '';
var $lines = '';
var $data = '';
var $chunk = '';
var $crc_table = '';
function Image()
{
session_start();
session_register("check_number");
$this-session = $_SESSION;
$this-is_gd = function_exists('gd_info') ? true : false;
$this-getCheckNumber();
if( $this-is_gd ):
$this-getFromGD();
else:
$this-getFromCode();
endif;
}
//'----浏览器输出
function outputBrowser()
{
if( $this-is_gd )
{
Header("Content-type: image/png"); //告诉浏览器,下面的数据是图片,而不要按文字显示
Imagepng($this-img_cache); //生成png格式。。。嘿嘿效果蛮像回事的嘛。。。
ImageDestroy($this-img_cache);
}
else echo($this-img_cache);
}
//'----文件输出
function outputFile($filename=null)
{
if(!$filename)$filename=time().'.png';
$fp = fopen($filename, 'w+');
fwrite($fp, $this-img_cache);
fclose($fp);
}
//'----得到验证码
function getCheckNumber()
{
//srand(microtime() * 100000);//PHP420后,srand不是必须的
for($Tmpa=0;$Tmpa$this-lenght;$Tmpa++)
{
$this-check_number.= $this-is_gd ? dechex(rand(0,15)) : rand(0,3);
}
unset($Tmpa);
$this-session['check_number'] = strtoupper($this-check_number);
}
//'----用GD库生成
function getFromGD()
{
//$black = '';
$this-img_cache = imageCreate($this-img_width,$this-img_height); //生成图片
//图片底色,ImageColorAllocate第1次定义颜色PHP就认为是底色了
$black = ImageColorAllocate($this-img_cache, 255,255,255);
//下面该生成雪花背景了,其实就是在图片上生成一些符号
//先用100个做测试
/*
for ($i=1; $i=15; $i++)
{
imageString($this-img_cache,1,mt_rand(1,$this-img_height-10),mt_rand(1,$this-img_width-10),".",
imageColorAllocate($this-img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));
//哈,看到了吧,其实也不是雪花,就是生成*号而已。为了使它们看起来"杂乱无章、5颜6色",就得在1个1个生成它们的时候,让它们的位置、颜色,甚至大小都用随机数,rand()或mt_rand都可以完成。
}
*/
//上面生成了背景,现在就该把已经生成的随机数放上来了。道理和上面差不多,随机数1个1个地放,同时让他们的位置、大小、颜色都用成随机数~~
//为了区别于背景,这里的颜色不超过200,上面的不小于200
imagettftext($this-img_cache, 20, 0, 10, 20, $black, "./html/VINERITC.TTF", "AC67");
for ($i=0;$istrlen($this-session['check_number']);$i++)
{
imagettftext($this-img_cache, 14, rand(0,30), $i*$this-img_width/4 + 3, 17, imageColorAllocate($this-img_cache,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)), "./html/VINERITC.TTF", $this-session['check_number'][$i]);
}
for($i=0;$i100;$i++)//加入干扰象素
{
imagesetpixel($this-img_cache, rand()%70 , rand()%30 , imageColorAllocate($this-img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));
}
$black = ImageColorAllocate($this-img_cache, 0,0,0); //定义需要的黑色
//先成一黑色的矩形把图片包围
ImageRectangle($this-img_cache,0,0, $this-img_width-1,$this-img_height-1, $black);
}
//End Function getGD
#-----------------------------------------------------------------------------------------------------------------------
function set_4pixel($r, $g, $b, $x, $y)
{
$ofs = 3 * ($this-img_width * $y + $x);
$this-pixels[$ofs] = chr($r);
$this-pixels[$ofs + 1] = chr($g);
$this-pixels[$ofs + 2] = chr($b);
$this-pixels[$ofs + 3] = chr($r);
$this-pixels[$ofs + 4] = chr($g);
$this-pixels[$ofs + 5] = chr($b);
$ofs += 3 * $this-img_width;
$this-pixels[$ofs] = chr($r);
$this-pixels[$ofs + 1] = chr($g);
$this-pixels[$ofs + 2] = chr($b);
$this-pixels[$ofs + 3] = chr($r);
$this-pixels[$ofs + 4] = chr($g);
$this-pixels[$ofs + 5] = chr($b);
}
//生成数字图象的函数
function draw2digits($x, $y, $number)
{
//$this-draw_digit($x, $y, intval($number / 10) );
$this-draw_digit($x + 11, $y, $number % 10);
}
function draw_digit($x, $y, $digit)
{
$digit = $this-digits[$digit];
$m = 8;
for($b = 1, $i = 0; $i 7; $i++, $b *= 2)
{
if(($b $digit) == $b)
{
$j = $i * 4;
$x0 = $this-lines[$j] * $m + $x;
$y0 = $this-lines[$j + 1] * $m + $y;
$x1 = $this-lines[$j + 2] * $m + $x;
$y1 = $this-lines[$j + 3] * $m + $y;
if($x0 == $x1)
{
$ofs = 3 * ($this-img_width * $y0 + $x0);
for($h = $y0; $h = $y1; $h++, $ofs += 3 * $this-img_width)
{
$this-pixels[$ofs] = chr(0);
$this-pixels[$ofs + 1] = chr(0);
$this-pixels[$ofs + 2] = chr(0);
}
}
else
{
$ofs = 3 * ($this-img_width * $y0 + $x0);
for($w = $x0; $w = $x1; $w++)
{
$this-pixels[$ofs++] = chr(0);
$this-pixels[$ofs++] = chr(0);
$this-pixels[$ofs++] = chr(0);
}
}
}//End if
}//End for
}
//将文字加入到图象中
function add_chunk($type)
{
// chunk :为层
// length: 4 字节: 用来计算 chunk
// chunk type: 4 字节
// chunk data: length bytes
// CRC: 4 字节: 循环冗余码校验
// copy data and create CRC checksum
$len = strlen($this-data);
$this-chunk = pack("c*", ($len 24) 255,
($len 16) 255,
($len 8) 255,
$len 255);
$this-chunk .= $type;
$this-chunk .= $this-data;
//calculate a CRC checksum with the bytes chunk[4..len-1]
$z = 16777215;
$z |= 255 24;
$c = $z;
for ($n = 4; $n strlen($this-chunk); $n++) {
$c8 = ($c 8) 0xffffff;
$c = $this-crc_table[($c ^ ord($this-chunk[$n])) 0xff] ^ $c8;
}
$crc = $c ^ $z;
$this-chunk .= chr(($crc 24) 255);
$this-chunk .= chr(($crc 16) 255);
$this-chunk .= chr(($crc 8) 255);
$this-chunk .= chr($crc 255);
//将结果加到$img_cache中
$this-img_cache .= $this-chunk;
}
//主程序
function getFromCode()
{
//填充
for($h = 0; $h $this-img_height; $h++)
{
for($w = 0; $w $this-img_width; $w++)
{
$r = 100 / $this-img_width * $w + 155;
$g = 100 / $this-img_height * $h + 155;
$b = 255 - (100 / ($this-img_width + $this-img_height) * ($w + $h));
$this-pixels .= chr($r);
$this-pixels .= chr($g);
$this-pixels .= chr($b);
}
}
$this-digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
$this-lines = array(1, 1, 1, 2, 0, 1, 0, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 0, 1, 0);
for($i=0; $i$this-lenght; $i++)
{
$this-draw2digits($i*13, 2, $this-check_number[$i]);
}
//$this-set_4pixel(0, 0, 0, 26, 7);
//$this-set_4pixel(0, 0, 0, 26, 13);
//$this-set_4pixel(0, 0, 0, 52, 7);
//$this-set_4pixel(0, 0, 0, 52, 13);
//创建循环冗余码校验表
$z = -306674912;// = 0xedb88320
for ($n = 0; $n 256; $n++)
{
$c = $n;
for ($k = 0; $k 8; $k++)
{
$c2 = ($c 1) 0x7fffffff;
if($c 1) $c = $z ^ ($c2);
else $c = $c2;
}
$this-crc_table[$n] = $c;
}
// PNG file signature
$this-img_cache = pack("c*", 137,80,78,71,13,10,26,10);
// IHDR chunk data:
// width: 4 bytes
// height: 4 bytes
// bit depth: 1 byte (8 bits per RGB value)
// color type: 1 byte (2 = RGB)
// compression method: 1 byte (0 = deflate/inflate)
// filter method: 1 byte (0 = adaptive filtering)
// interlace method: 1 byte (0 = no interlace)
$this-data = pack("c*", ($this-img_width24) 255,
($this-img_width 16) 255,
($this-img_width 8) 255,
$this-img_width 255,
($this-img_height 24) 255,
($this-img_height 16) 255,
($this-img_height 8) 255,
$this-img_height 255, 8, 2, 0, 0, 0);
$this-add_chunk("IHDR");
//以下不敢乱翻译,请自行参考
// scanline:
// filter byte: 0 = none
// RGB bytes for the line
// the scanline is compressed with "zlib", method 8 (RFC-1950):
// compression method/flags code: 1 byte ($78 = method 8, 32k window)
// additional flags/check bits: 1 byte ($01: FCHECK = 1, FDICT = 0, FLEVEL = 0)
// compressed data blocks: n bytes
// one block (RFC-1951):
// bit 0: BFINAL: 1 for the last block
// bit 1 and 2: BTYPE: 0 for no compression
// next 2 bytes: LEN (LSB first)
// next 2 bytes: one's complement of LEN
// LEN bytes uncompressed data
// check value: 4 bytes (Adler-32 checksum of the uncompressed data)
$len = ($this-img_width * 3 + 1) * $this-img_height;
$this-data = pack("c*", 0x78, 0x01, 1, $len 255, ($len8) 255, 255 - ($len 255), 255 - (($len 8) 255) );
$start = strlen($this-data);
$i2 = 0;
for($h = 0; $h $this-img_height; $h++)
{
$this-data .= chr(0);
for($w = 0; $w $this-img_width * 3; $w++)
{
$this-data .= $this-pixels[$i2++];
}
}
//calculate a Adler32 checksum with the bytes data[start..len-1]
$s1 = 1;
$s2 = 0;
for ($n = $start; $n strlen($this-data); $n++)
{
$s1 = ($s1 + ord($this-data[$n])) % 65521;
$s2 = ($s2 + $s1) % 65521;
}
$adler = ($s2 16) | $s1;
$this-data .= chr(($adler 24) 255);
$this-data .= chr(($adler 16) 255);
$this-data .= chr(($adler 8) 255);
$this-data .= chr($adler 255);
$this-add_chunk("IDAT");
//IEND: marks the end of the PNG-file
$this-data = "";
$this-add_chunk("IEND");
//列印图象
//echo($this-img_cache);
}
}//End Class
?
相关问答
Q1: PHP数据库验证登陆
你记住 所有在 引号里面的 东西都是字符串!!!!!
if("SELECT username,passwd FROM wen WHERE $username=$user,$passwd=$passwd"){
echo "scriptalert('欢迎登录!');window.location.href='index1.php';/script";
}
这一句代码本身就是不通的.
还有就是 你的sql语句也是错误的。
参考如下:
$sql = 'SELECT username,passwd FROM wen WHERE username="'.$user.'" AND passwd="'.$passwd.'"';
$result = mysql_query($sql,$conn);
手动回复,不谢
Q2: php foreach 返回问题
?php
/**
* 不是很看php数据验证类的懂你这个程序是干啥php数据验证类的php数据验证类,英文字面上意思是数据验证的,源代码太长很多不必要的地方
* Class Validate
*/
class Validate{
public function data_verify($_verify){
//必要数据验证
if(empty($_verify)) return false;
//报错行数计算
$num = 0;
//正确代码数组
$successData = array('11'='test','22'='test1','33'='test2','44'='test3');
//遍历验证
foreach($_verify as $key=$value){
//正确进入
if(!empty($successData[$value]))
{
$data['content'] = $successData[$value];
}else{
//如果有错误会在这里输出,当然你可以有错误就直接返回return false;或者记录在报错数组! 你要什么逻辑代码自己改喽~
echo '第'.$num.'个数组下标有误!';
}
$num++;
}
return $data;
}
}
$validate = new Validate();
$request=$validate-data_verify(array('test'='11','test1'='22','test2'='33','test3'='44'));
var_dump($request);
Q3: thinkphp验证器正则常用规则
ThinkPHP的自动验证常用的正则
一般php数据验证类我们见的比较多的是设置规则为require、email之类的php数据验证类,其实这些本身也是属于正则表达式验证方式,只是系统内置定义了一些常用的正则表达式而已。这些内置的正则表达式的定义可以参考model类的regex方法,内置支持的正则定义包括:
require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字、zip 邮编、integer 整数、double 浮点数、english 英文字母,但是并不局限于这些正则规则的,我们完全可以直接在验证规则里面使用正则表达式进行定义,这样我们可以凭借强大的正则表达式来进行表单字段验 证,例如:
附上一些表单验证中比较常用的正则表达式写法:
匹配特定字符串:
限定符
限定符用来指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。有 * 或 + 或 ? 或 {n} 或 {n,} 或 {n,m} 共6种。
正则表达式的限定符有:
ThinkPHP的自动验证机制是为了进行表单数据验证,验证可以支持function、 callback、confirm、equal、unique和regex,这里要讲的是使用正则表达式进行验证。
特殊字符
许多元字符要求在试图匹配它们时特别对待。若要匹配这些特殊字符,必须首先使字符"转义",即,将反斜杠字符\ 放在它们前面。下表列出了正则表达式中的特殊字符:
正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
例如:
构造正则表达式的方法和创建数学表达式的方法一样。也就是用多种元字符与运算符可以将小的表达式结合在一起来创建更大的表达式。正则表达式的组件可以是单个的字符、字符集合、字符范围、字符间的选择或者所有这些组件的任意组合。
正则表达式是由普通字符(例如字符 a 到 z)以及特殊字符(称为"元字符")组成的文字模式。模式描述在搜索文本时要匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。
普通字符包括没有显式指定为元字符的所有可打印和不可打印字符。这包括所有大写和小写字母、所有数字、所有标点符号和一些其php数据验证类他符号。
非打印字符也可以是正则表达式的组成部分。下表列出了表示非打印字符的转义序列:
所谓特殊字符,就是一些有特殊含义的字符,如上面说的 runoo*b 中的 ,简单的说就是表示任何字符串的意思。如果要查找字符串中的 * 符号,则需要对 * 进行转义,即在其前加一个 : runo*ob 匹配 runo ob。
许多元字符要求在试图匹配它们时特别对待。若要匹配这些特殊字符,必须首先使字符"转义",即,将反斜杠字符\ 放在它们前面。下表列出了正则表达式中的特殊字符:
限定符用来指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。有 * 或 + 或 ? 或 {n} 或 {n,} 或 {n,m} 共6种。
正则表达式的限定符有:
由于章节编号在大的输入文档中会很可能超过九,所以您需要一种方式来处理两位或三位章节编号。限定符给您这种能力。下面的正则表达式匹配编号为任何位数的章节标题:
请注意,限定符出现在范围表达式之后。因此,它应用于整个范围表达式,在本例中,只指定从 0 到 9 的数字(包括 0 和 9)。
这里不使用 + 限定符,因为在第二个位置或后面的位置不一定需要有一个数字。也不使用 ? 字符,因为使用 ? 会将章节编号限制到只有两位数。您需要至少匹配 Chapter 和空格字符后面的一个数字。
如果您知道章节编号被限制为只有 99 章,可以使用下面的表达式来至少指定一位但至多两位数字。
上面的表达式的缺点是,大于 99 的章节编号仍只匹配开头两位数字。另一个缺点是 Chapter 0 也将匹配。只匹配两位数字的更好的表达式如下:
或
* 、+限定符都是贪婪的,因为它们会尽可能多的匹配文字,只有在它们的后面加上一个?就可以实现非贪婪或最小匹配。
例如,您可能搜索 HTML 文档,以查找括在 H1 标记内的章节标题。该文本在您的文档中如下:
H1Chapter 1 - 介绍正则表达式/H1
贪婪: 下面的表达式匹配从开始小于符号 () 到关闭 H1 标记的大于符号 () 之间的所有内容。
非贪婪: 如果您只需要匹配开始和结束 H1 标签,下面的非贪婪表达式只匹配 H1。
如果只想匹配开始的 H1 标签,表达式则是:
通过在 *、+ 或 ? 限定符之后放置 ?,该表达式从"贪心"表达式转换为"非贪心"表达式或者最小匹配。
定位符使您能够将正则表达式固定到行首或行尾。它们还使您能够创建这样的正则表达式,这些正则表达式出现在一个单词内、在一个单词的开头或者一个单词的结尾。
正则表达式的定位符有:
php数据验证类的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php验证码类、php数据验证类的信息别忘了在本站进行查找喔。







