程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php驗證碼

php驗證碼

編輯:關於PHP編程

<? 
/**
 * 驗證碼
 * 2011/8/21
 * kcj
 * */ 
include "isLogin.php"; 
//隨機生成一個4位數字的驗證碼  
$num=''; 
for($i=0;$i<4;$i++){ 
    $num.=dechex(rand(0,20));  //dechex函數是十進制轉會二進制  

session_start();   //開啟session  
$_SESSION['yanzheng']=$num;  //用session記住這個驗證數字  
header("Content-type:image/PNG"); 
$im=imagecreate(60,20);  //創建一個畫布  
$back=imagecolorallocate($im,rand(0,55),rand(0,20),rand(0,5));//創建一個背景顏色 (黑色)  
$gray=imagecolorallocate($im,rand(0,255),rand(0,200),rand(0,55)); //(白色)  
imagefill($im,0,0,$gray);   //填充顏色  
$style=array($back,$back,$back,$back,$back,$gray,$gray,$gray,$gray,$gray); //生成數組  
imagesetstyle($im,$style);    //設定畫線風格  
$y1=rand(0,20); 
$y2=rand(0,20); 
$y3=rand(0,20); 
$y4=rand(0,20); 
imageline($im,0,$y1,60,$y3,IMG_COLOR_STYLED);     //畫一條線  
imageline($im,0,$y2,60,$y4,IMG_COLOR_STYLED); 
//在畫布上隨機生成大量黑點,起干擾作用  
for ($i=0;$i<80;$i++){ 
    imagesetpixel($im,rand(0,60),rand(0,20),$back); 

$str=rand(3,8); 
for ($i=0;$i<4;$i++){ 
    $strp=rand(1,6); 
    imagestring($im,6,$str,$strp,substr($num,$i,1),$back); 
    $str+=rand(8,12); 

ImagePNG($im); 
imagedestroy($im); 
 
?> 

摘自 chaojie2009的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved