PHP驗證碼代碼分享

來源:互聯網
上載者:User

標籤:

<?php 
session_start();   //在檔案的最開頭開啟session 
//把英文和中文分別放入兩個數組中 
$english_arr=array(‘q‘,‘w‘,‘e‘,‘r‘,‘t‘,‘y‘,‘u‘,‘i‘,‘o‘,‘p‘,‘a‘,‘s‘,‘d‘,‘f‘,‘g‘,‘h‘,‘j‘, 
                        ‘k‘,‘l‘,‘z‘,‘x‘,‘c‘,‘v‘,‘b‘,‘n‘,‘m‘); 
$chinese_arr=array(‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘,‘七‘,‘八‘,‘九‘,‘十‘,‘零‘,‘木‘,‘和‘,‘我‘, 
                        ‘人‘,‘有‘,‘的‘,‘右‘,‘主‘,‘產‘,‘為‘,‘這‘,‘工‘,‘上‘,‘下‘,‘左‘); 
/** 
* 取得中文和英文 
*/ 

/** 
* 取得一個隨機數, 隨機字母,隨機中文 
*/ 
for($i=0;$i<5;$i++) 
{ 
    $index=rand(0,2); 
    if($index==‘0‘) 
    { 
        $num.=rand(0,9); 
//        echo "<font color=red>$num</font><br>"; 
    } 
    else if($index==‘1‘) 
    { 
        $num.=$english_arr[rand(0,25)]; 
//        echo "<font color=blue>$num</font><br>"; 
    } 
    else if($index==‘2‘) 
    { 
//        echo $num; 
        $num.=$chinese_arr[rand(0,25)];   //如果加入的是中文,則要轉換編碼 
//        echo "<font color=green>$num</font><br>"; 
    } 

} 

$_SESSION[check_num]=$num;  //把這個驗證碼記入session中 
//echo $_SESSION[check_num]; 
//  imagecreatetruecolor(寬,高) 
$image=imagecreatetruecolor(150,50);   //建立一個100,30的圖片 

//  imagecolorallocate(圖片名,紅,藍,綠)   三原色 
imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));//第一次是設定圖片背景色,三原色隨機 

$color=imagecolorallocate($image,255,255,255);//以後的為了建立一種顏色 

// imagestring(圖片名,字型類型1-6六種,X座標,Y座標,內容,顏色) 
//imagestring($image,rand(3,6),10,3,$num,$color);   //把內容寫到圖片中  不能輸出中文 

//  imagettftext(圖片名,字型大小,位移角度,X座標,Y座標,顏色,字型檔地址,輸入的內容)  這個內容必須要在輸入的時候轉換編碼 
imagettftext($image,rand(15,18),0,rand(2,50),rand(30,39),$color,‘simkai.ttf‘,iconv(‘GBK‘,‘UTF-8‘,$num));   //把內容寫到圖片中,可以有中文 



/** 
* 畫線和點做幹擾 
*/ 
for($i=0;$i<10;$i++) 
{ 
     $linecolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); 
     imageline($image,rand(0,150),rand(0,50),rand(0,150),rand(0,50),$linecolor); 

} 
  for($i=0;$i<200;$i++) 
{ 
     $pixelcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); 
      imagesetpixel($image,rand(0,150),rand(0,50),$pixelcolor); 

} 

/** 
  * 輸出圖片 
  */ 
//echo dechex(rand(0,15)); 

header("ContentType:image/jpeg");   //設定此檔案是以什麼形勢輸出   這裡是作為一張圖片 

imagejpeg($image);  //輸出jpeg格式的圖片 



?>

PHP驗證碼代碼分享

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.