php產生驗證碼圖片從入門和精通教程_PHP教程

來源:互聯網
上載者:User
在php中要產生驗證碼圖片是相當的簡單的,因為在php中為我們提供了圖形gd.dll庫,要啟用gd圖形庫我們只要在在php.ini中把php-gd前面的;去就可以了。

方法一

代碼如下 複製代碼

$authnum='';
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);//分割函數
for($i=0;$i<4;$i++){
$randnum=rand(0,35);
$authnum.=$list[$randnum];//以數組的形式輸出

方法二:

代碼如下 複製代碼
private function createCheckCode()
{
for(i=0;icodeNum;i++)
{
number = rand(0,2);
switch(number)
{
case 0: rand_number = rand(48,57); break;//數字
case 1: rand_number = rand(65,90);break;//大寫字母
case 2: rand_number = rand(97,122);break;//小寫字母
}
$asc = sprintf("%c",rand_number);
$asc_number = asc_number.asc;
}
return asc_number;
}

方法三:

代碼如下 複製代碼
srand(microtime()*100000);//相當於計時器
$string="abcdefghigklmnopqrstuvwxyz123456789";
for($i=0;$i<4;$i++)
{
$new_number.=$string[rand(0,strlen($string)-1)];//隨即的產生一個數組
}

方法四:

代碼如下 複製代碼

for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));//將十進位轉化為十六進位
}


隨機產生數字,字母的代碼:

代碼如下 複製代碼
//che.php
session_start();
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));
}
$_SESSION['check_num']=$rand;
$image=imagecreatetruecolor(50,30);
$bg=imagecolorallocate($im,0,0,0);//第一次用調色盤的時候,背景顏色
$te=imagecolorallocate($im,255,255,255);
imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
ob_clean();//PHP網頁中因為 要產生驗證碼而出現 映像"http://localhost/**.php"因其本身有錯無法顯示
header("Content-type:image/jpeg"); imagejpeg($image);
?>

給圖片畫出幹擾線代碼:

代碼如下 複製代碼
for($i=0;$i<8;$i++)//畫出多條線
{
$cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//產生隨機的顏色
imageline($im,rand(10,40),0,rand(10,40),20,$cg);
}

給圖片畫出幹擾點的代碼:

代碼如下 複製代碼
for($i=0;$i<80;$i++)//畫出多個點
{
imagesetpixel($im,rand(0,40),rand(0,20),$cg);
}

把文字寫入圖片代碼:

代碼如下 複製代碼

$str=array('我','我','親','親');//儲存顯示的漢字
for($i=0;$i<4;$i++)
{
$sss.=$str[rand(0,3)];//隨機顯示漢字
}

//$str=iconv("gb2312","utf-8",$str); //漢字編碼轉化,我的好像不需要
imagettftext($im,10,0,rand(5,60),rand(5,60),$te,"simhei.ttf",$sss);//

最後我們結合實際分享一個完整的執行個體

代碼如下 複製代碼
/**
* 產生驗證碼圖片
*
* @param String $word 驗證碼在session中的變數名稱
*/
function valiCode($word='randcode'){
Header("Content-type: image/gif");
$border = 0; //是否要邊框 1要:0不要
$how = 4; //驗證碼位元
$w = $how*15; //圖片寬度
$h = 18; //圖片高度
$fontsize = 10; //字型大小
$alpha = "abcdefghijkmnpqrstuvwxyz"; //驗證碼內容1:字母
$number = "23456789"; //驗證碼內容2:數字
$randcode = ""; //驗證碼字串初始化
srand((double)microtime()*1000000); //初始化隨機數種子
$im = ImageCreate($w, $h); //建立驗證圖片
/*
* 繪製基本架構
*/
$bgcolor = ImageColorAllocate($im, 255, 255, 255); //設定背景顏色
ImageFill($im, 0, 0, $bgcolor); //填充背景色
if($border)
{
$black = ImageColorAllocate($im, 0, 0, 0); //設定邊框顏色
ImageRectangle($im, 0, 0, $w-1, $h-1, $black);//繪製邊框
}

/*
* 逐位產生隨機字元
*/
for($i=0; $i<$how; $i++)
{
$alpha_or_number = mt_rand(0, 1); //字母還是數字
$str = $alpha_or_number ? $alpha : $number;
$which = mt_rand(0, strlen($str)-1); //取哪個字元
$code = substr($str, $which, 1); //取字元
$j = !$i ? 4 : $j+15; //繪字元位置
$color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //字元隨即顏色
ImageChar($im, $fontsize, $j, 3, $code, $color3); //繪字元
$randcode .= $code; //逐位加入驗證碼字串
}

/*
* 如果需要添加幹擾就將注釋去掉
*
* 以下for()迴圈為繪背景幹擾線代碼
*/
/* + -------------------------------繪背景幹擾線 開始-------------------------------------------- + */
for($i=0; $i<5; $i++)//繪背景幹擾線
{
$color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //幹擾線顏色
ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); //幹擾線
}
/* + -------------------------------繪背景幹擾線 結束-------------------------------------- + */

/*
* 如果需要添加幹擾就將注釋去掉
*
* 以下for()迴圈為繪背景幹擾點代碼
*/
/* + --------------------------------繪背景幹擾點 開始------------------------------------------ + */

for($i=0; $i<$how*40; $i++)//繪背景幹擾點
{
$color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //幹擾點顏色
ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); //幹擾點
}

/* + --------------------------------繪背景幹擾點 結束------------------------------------------ + */

//把驗證碼字串寫入session 方便提交登入資訊時檢驗驗證碼是否正確 例如:$_POST['randcode'] = $_SESSION['randcode']
$_SESSION[$word] = $randcode;
/*繪圖結束*/
Imagegif($im);
ImageDestroy($im);
/*繪圖結束*/
}

調用方法也很簡單把上面執行個體儲存img.php檔案,然後在要調用的頁面
html頁面如下

代碼如下 複製代碼

這要就可以實現驗證碼調用了。

再加個提交驗證驗證碼是否正確

代碼如下 複製代碼

session_start();
if((strtoupper($_POST["code"])) == strtoupper(($_SESSION["VerifyCode"]))){
print("驗證碼正確,");
}else{
print("驗證碼錯誤,");
}

?>

這要就我們完成了從產生驗證碼圖片和使用的整個過程了,也算是從php入門到精通驗證碼全部講了。

http://www.bkjia.com/PHPjc/631625.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631625.htmlTechArticle在php中要產生驗證碼圖片是相當的簡單的,因為在php中為我們提供了圖形gd.dll庫,要啟用gd圖形庫我們只要在在php.ini中把php-gd前面的;去就可以...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.