本文目錄一覽:
求一個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
?
怎麼用PHP寫個驗證碼
首先,當用戶打開頁面時隨機產生一個session,然後根據這個值生成驗證碼圖片。
第二,將驗證碼圖片顯示到表單上。
第三,當用戶提交時表單時,比較session里的值與表單中驗證碼的值進行比較。
簡單的實現過程:
複雜的驗證碼圖片生成:
php驗證碼怎麼實現
1. 新建code.php驗證碼生成文件
在此之前必須打開php的GD庫,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分號。代碼如下:
?php
session_start();
//生成驗證碼圖片
Header(“Content-type: image/PNG”);
$im = imagecreate(44,18);
$back = ImageColorAllocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景
srand((double)microtime()*1000000);
//生成4位數字
for($i=0;$i4;$i++){
$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}
for($i=0;$i100;$i++) //加入干擾象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()p , rand()0 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
$_SESSION[‘Checknum’] = $vcodes;
?
2. 顯示驗證碼圖片
在需要顯示驗證碼的頁面中加入
input type=”text” name=”passcode”
img src=”code.php”
3.判斷並獲取驗證碼的值
驗證碼是通過第一步驟代碼中的$_SESSION[‘Checknum’] = $vcodes;賦的值,所以驗證碼的值存在$_SESSION[‘Checknum’]當中。在驗證頁面,使用以下代碼,
…
session_start();//啟動會話
$code=$_POST[“passcode”];
if( $code == $_SESSION[“Checknum”])
{…}即可完成驗證碼登錄。
運行截圖:
望採納,謝謝
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127444.html