内容导航:$data[$i][$j]=0,++$i) { for($j=0,//输出验证码 for ($i=0,$sign[$j] .= $binstr{$base + $i * $column}
{image}
一、PHP如何将一个验证码图片内容读取出来都是数字
例如如何将下面的图片获取实际的显示数字/zhmap/?id=2&flag;=1如果php能读取到图片上的文字字符,那网站上登陆注册等弄验证码 ,就是多此一举
验证码的初衷就是为了防止一些软件恶意注册或者灌水
而你的这个要求,显然与这个目的相互违背!你把验证码最终结果写到session里,然后post过来的数跟 session 里面的对比 ,相同为真,不能就是错误
比如 验证码为 1+4 那么把 5写到session 里,然后跟 post 过来的对比 ,就成了用图像识别技术。
二 、php识别验证码
/这个验证码如何使用PHP自动识别并输出文字 在网上找到2个PHP识别程序 但是不会修改里面的代码其中一个显示 不正确
/ceshi/这个地址是我自己传到空间以后 测试的效果 上面输出的文字跟图片对不上
另外一个程序/phpcrash/直接显示错误下面我提供他们的下载地址/:///希望明白的人给于指导我个人感觉应该是程序需要修改设置
但是我看不是很明白里面的代码上面提供的地址是我自己空间的setImage(' /'); //引号内原值为 $valite->getHec();$ert =
$valite->run();//$ert = "1234";print_r($ert);echo '
'; //引号内 这个部分我自己修改过 原值为 ?>=================以上代码为
内容===========================ImagePath = $Image; } public function getData() {
return $data; } public function getResult() { return $DataArray; } public
function getHec() { $res = imagecreatefromjpeg($this->ImagePath); $size =
getimagesize($this->ImagePath); $data = array(); for($i=0; $i < $size[1];
++$i) { for($j=0; $j < $size[0]; ++$j) { $rgb = imagecolorat($res,$j,$i);
$rgbarray = imagecolorsforindex($res, $rgb); if($rgbarray['red'] < 125 ||
$rgbarray['green']<125 || $rgbarray['blue'] < 125) { $data[$i][$j]=1; }else{
$data[$i][$j]=0; } } } $this->DataArray = $data; $this->ImageSize = $size; }
public function run() { $result=""; // 查找4个数字 $data = array("","","","");
for($i=0;$i<4;++$i) { $x = ($i*(WORD_WIDTH+WORD_SPACING))+OFFSET_X; $y =
OFFSET_Y; for($h = $y; $h < (OFFSET_Y+WORD_HIGHT); ++ $h) { for($w = $x; $w <
($x+WORD_WIDTH); ++$w) { $data[$i].=$this->DataArray[$h][$w]; } } } // 进行关键字匹配
foreach($data as $numKey => $numString) { $max=0.0; $num = 0;
foreach($this->Keys as $key => $value) { $percent=0.0; similar_text($value,
$numString,$percent); if(intval($percent) > $max) { $max = $percent; $num =
$key; if(intval($percent) > 95) break; } } $result.=$num; } $this->data =
$result; // 查找最佳匹配数字 return $result; } public function Draw() { for($i=0;
$i<$this->ImageSize[1]; ++$i) { for($j=0; $j<$this->ImageSize[0]; ++$j) { echo
$this->DataArray[$i][$j]; } echo "\n"; } } public function __construct() {
$this->Keys = array( '0'=>'', '1'=>'', '2'=>'', '3'=>'', '4'=>'', '5'=> '',
'6'=>'', '7'=>'', '8'=>'', '9'=>'', ); } protected $ImagePath; protected
$DataArray; protected $ImageSize; protected $data; protected $Keys; protected
$NumStringArray;}?>==============以上内容为
另外一个程序差不多需要的话自己下载看看比较清楚一些吧为什么没人来看啊这个代码获取的不准确啊 。有没有样式希求,没有的话我自己写了一个验证码。发给你PHP:
error_reporting(E_ALL);
class PNG_READER {
var $pngdata;
var $offset;
var $width;
var $height;
var $bitdepth; //bit depth
var $rowbytes;
var $irowbytes;
var $colortype;
var $bmpdata;
var $code = '';
function PNG_READER(&$data)
{
$this->pngdata = $data;
$this->offset = 0;
}
function _read_raw($len)
{
$start = $this->offset;
$this->offset += $len;
return substr($this->pngdata, $start, $len);
}
//return tag name and data
function _read_chunk(&$tag)
{
$chunk = unpack('Nlength', $this->_read_raw(4));
$data = $this->_read_raw($chunk['length'] + 4);
$chunk = unpack('Ncrc', $this->_read_raw(4));
if ($chunk['crc'] !== crc32($data)) {
exit("crc error\n");
}
$tag = substr($data, 0, 4);
return substr($data, 4);
}
function read()
{
$signature = $this->_read_raw(8);
$sign_str = pack('C8', 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A);
if ($signature != $sign_str) {
exit("invalid signature\n");
}
$tag = '';
do {
$data = $this->_read_chunk($tag);
switch($tag) {
case 'IHDR':
$fileheader =
unpack('Nwidth/Nheight/Cdepth/Ctype/Ccompression/Cfilter/Cinterlace', $data);
$this->bitdepth = $fileheader['depth']; //2 == 2 palette index ,refrer to PLTE
chunk
//the sample depth is always 8 bits.
$this->colortype = $fileheader['type']; //3 == indexed-color
$this->width = $fileheader['width'];
$this->height = $fileheader['height'];
$this->rowbytes = (($fileheader['depth'] * $fileheader['width']) + 7)>> 3;
//$this->row_bytes = ($fileheader['depth'] * $fileheader['width'])>> 3;
break;
case 'IDAT':
$this->irowbytes = $this->rowbytes + 1;
$size = ($this->irowbytes) * $this->height;
$this->bmpdata = gzuncompress($data);
break;
case 'PLET':
break;
default:
break;
}
} while('IEND' !== $tag);
unset($data);
}
function _convert_hex_binstr($hex)
{
$bin['0'] = ' ';
$bin['1'] = ' 1';
$bin['2'] = ' 1 ';
$bin['3'] = ' 11';
$bin['4'] = ' 1 ';
$bin['5'] = ' 1 1';
$bin['6'] = ' 11 ';
$bin['7'] = ' 111';
$bin['8'] = '1 ';
$bin['9'] = '1 1';
$bin['a'] = '1 1 ';
$bin['b'] = '1 11';
$bin['c'] = '11 ';
$bin['d'] = '11 1';
$bin['e'] = '111 ';
$bin['f'] = '1111';
return $bin[$hex];
}
function analyze_row()
{
$binstr = '';
$hexstr = bin2hex($this->bmpdata);
unset($this->bmpdata);
$hexlen = $this->irowbytes * 2;
for ($i = 0; $i <$this->height; $i ++) {
//echo bin2hex(substr($this->bmpdata, $i * $this->irowbytes,
$this->irowbytes));
for ($j = 0; $j <$hexlen; $j ++) {
$pos = $hexlen * $i + $j;
$tmpstr = $this->_convert_hex_binstr($hexstr{$pos});
$binstr .= $tmpstr;
//echo $tmpstr;
}
//echo "\n";
}
$this->_reconstruct($binstr, $hexlen*4, $this->height-1);
}
function _reconstruct($binstr, $column, $row)
{
$base = 29;
for ($j = 0; $j <4; $j ++) {
$sign[$j] = '';
for ($i = 9; $i <$row; $i ++) {
$sign[$j] .= $binstr{$base + $i * $column};
}
$base += 14;
$rcode = $this->_convert_signature_to_num($sign[$j]);
if (0 == $rcode) {
$dot = 11 * $column + 29 + 14 * $j;
if ($binstr{$dot+8} == 0) {
$rcode = 6;
}
}
$this->code .= $rcode;
}
}
function _convert_signature_to_num($str)
{
$fingermark[' '] = 0;
$fingermark[' 1 1'] = 1;
$fingermark[' 11 11'] = 2;
$fingermark[' 1 1 '] = 3;
$fingermark[' 11 '] = 4;
$fingermark['1111 1 '] = 5;
$fingermark['1 '] = 7;
$fingermark[' 11 111 '] = 8;
$fingermark[' 111 1 '] = 9;
return $fingermark[$str];
}
}
$filedata = file_get_contents(' /pagemydns/');
$png = & new PNG_READER($filedata);
$png->read();
$png->analyze_row();
print_r($png->code);
echo "\n";
你直接发代码来看.
三、PHP识别网页验证码的实例有那位大神有通用性比较好的给出完整
function _code($_width = 75,$_height = 25,$_rnd_code = 4,$_flag = false){ //创建随机码 for ($i=0;$i<$_rnd_code;$i++){ $_nmsg .= dechex(mt_rand(0, 15)); } //保存在session $_SESSION['code'] = $_nmsg;
//创建一张图像 $_img = imagecreatetruecolor($_width,$_height); //白色 $_white = imagecolorallocate($_img,255,255,255); //填充 imagefill($_img,0,0,$_white); if ($_flag){ //黑色,边框 $_black = imagecolorallocate($_img,0,0,0); imagerectangle($_img,0,0,$_width-1,$_height-1,$_black); } //随机画出六个线条 for ($i=0;$i<6;$i++){ $_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color); } //随机雪花 for ($i=0;$i<100;$i++){ $_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color); } //输出验证码 for ($i=0;$i
}
封装了
记得要在调用的页面开启 session_start();
PHP的Discuz!论坛架设方法 ,架设Discuz!论坛不但需要服务器支持PHP而且需要MYSQL数据库的支持。
PHP主机架设很复杂 ,这里就不介绍了,提供一个PHP环境包下载,下到本地解压、启动就行了 ,简单一些 。Apache+PHP+MySQL+Nginx+Memcached+ASP一键环境包: /design/?s=lily 解压缩后把Discuz!文件夹改名为BBS复制到你所建的Web主目录(web)中,打开浏览器输入
http://您的IP/bbs/就会出现Discuz!论坛的安装向导。 选择完全同意,然后出现选择数据库的选项。 轻松用建站宝盒架设自己的论坛网站/design/?s=lily
四 、php图片验证码实现
最好封装成类 ,可直接调用 。 可以用php的GD库做 //随机生成验证码 class randomString { function createRandomStr($strLen) { list($usec, $sec) = explode(' ', microtime()); (float) $sec + ((float) $usec * ); $number = ''; $number_len = $strLen; $stuff = 'abcdefghijklmnopqrstuvwxyz';//附加码显示范围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;$i<200;$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);