php產生帶幹擾的驗證碼程式

來源:互聯網
上載者:User
 代碼如下 複製代碼

<?php教程
/*
 * Created on 2011-3-11
 * Programmer : xiaoyao, QQ:1045195056
 驗證通過判斷輸入值與$_SESSION['check_pic']值
 */
session_start();
 function RandAscii($number){//$number產生數字和字母個數
$arr=array('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','w','v','u','x','y','z');
for ($i=1;$i<=$number;$i++)
{
$rand= $rand.$arr[rand(0,35)];
}
return $rand;
}
 $rand=RandAscii(4);

$_SESSION['check_pic']=$rand;//隨機產生的四個數賦值session中,用於驗證。
$x=80;
$y=24;
 $im=imagecreatetruecolor($x,$y);//建立圖片
$bg=imagecolorallocate($im,255,255,255);//設定顏色背景
imagefill( $im,0,0,$bg);
$wh=imagecolorallocate($im,255,255,0);
$grey=imagecolorallocate($im,128,128,128);
$yellow=imagecolorallocate($im,255,255,0);
$red=imagecolorallocate($im,0,255,0);
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
  imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
  imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
  imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
 );//字顏色數組
 //畫邊框
 $border = imagecolorallocate($im, 133, 153, 193);
 imagerectangle($im, 0, 0, $x - 1, $y - 1, $border);

for($i=0;$i<10;$i++){        //畫幹擾線,10條
imageline($im,rand(0,60),2,rand(0,60),20,$yellow);

}
for($j=0;$j<100;$j++){
 imagesetpixel($im,rand()%76,rand()%20,$red);
}
//imagestring($im,6,15,8,$rand,$wh);//字型大小1-5
imagettftext($im, 14,rand(30, -30), 5, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[0]);
imagettftext($im, 14,rand(50, -50), 24, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[1]);
imagettftext($im, 14,rand(50, -50), 43, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[2]);
imagettftext($im, 14,rand(30, -30), 62, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[3]);

header("Content-type: image/jpeg");//輸出圖片
imagejpeg($im);
imagedestroy($im);
?>

調用方法

 代碼如下 複製代碼
<?php
/*
 * Created on 2011-3-11
 * Programmer : xiaoyao, QQ:1045195056
 驗證通過判斷輸入值與$_SESSION['check_pic']值
 */
session_start();//開啟session
if(isset($_POST['check']))
{
if($_POST['check'])
 {
if($_POST['check']==$_SESSION['check_pic'])
 {
 echo " 驗證碼正確".$_SESSION['check_pic'];
 }
else
 {
 echo " 驗證碼錯誤".$_SESSION['check_pic'];
 }
}
}
?>
<FORM METHOD=POST ACTION="">
<img src="index.php"><br>    <!----連結圖片--->
<input type="text" name="check" >
<input type="submit" value="提交">
</FORM>

 

相關文章

聯繫我們

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