1. 程式人生 > >PHP實現驗證碼制作

PHP實現驗證碼制作

php 驗證碼

captcha.php(PHP產生驗證碼並儲存Session):

<?php



//開啟Session
session_start();



//繪制底圖
$image = imagecreatetruecolor(100, 30);//返回資源型的值
$bgcolor = imagecolorallocate($image, 255, 255, 255);//創建一個底圖
imagefill($image, 0, 0, $bgcolor);//區域填充



/*
//輸出隨機數字
for($i = 0; $i < 4; $i++){
	$fontsize = 6;//字體大小
	$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//字體顏色
	$fontcontent = rand(0, 9);//字符串內容
	
	$x = ($i*100/4) + rand(5, 10);//數字的橫坐標
	$y = rand(5, 10);//數字的縱坐標
	
	imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);//在圖像資源上繪制字符
}
*/



//產生隨機字符串
$captch_code = ‘‘;
for($i = 0; $i < 4; $i++){
	$fontsize = 6;//字體大小
	$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120),rand(0, 120));//字體顏色

	$data = ‘23456789ABCDEFGHJKLMNOPQRTUVWXYZabcdefghjkmnopqrtuvwxy‘;//隨機字符串的字典
	$fontcontent = substr($data, rand(0, strlen($data)), 1);//字符
	$captch_code .= $fontcontent;//拼接字符串
	
	$x = ($i*100/4) + rand(5, 10);//字符橫坐標
	$y = rand(5, 10);//字符縱坐標
	
	//在圖像資源上繪制字符
	imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
//將驗證碼字符串存儲在Session
$_SESSION[‘authcode‘] = $captch_code;




//點幹擾
for($i = 0; $i < 200; $i++){
	$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
	imagesetpixel($image, rand(1, 99), rand(1, 99), $pointcolor);
}



//線幹擾
for($i = 0; $i < 6; $i++){
	$linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
	imageline($image, rand(1, 99), rand(1, 99), rand(1, 99), rand(1, 99), $linecolor);
}



//輸出圖片內容
header(‘content-type:image/png‘);//輸出內容的格式
imagepng($image);//輸出內容
imagedestroy($image);//銷毀資源





?>




captcha-form.html(Web表單驗證):

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>驗證碼</title>
</head>

<body>
  <form method="post" action="./captcha-result.php">
    
	<p>
	  驗證碼圖片:
	  <img id="captcha-img" border="1" src="./captcha.php?r=0">
	  <a href="javascript:void(0);"  id="authcode" type="text" name="authcode" value=""></p>
	
	<p><input type="submit" value="提交" style="padding:6px 20px;"></p>
  
  </form>

  <script>
    //動態獲取驗證碼
    function getCaptchaImg(){
		//從服務器獲取新的驗證碼
		document.getElementById(‘captcha-img‘).src=‘./captcha.php?r=‘+Math.random();
		//清空文本框裏已輸入的內容
		document.getElementById(‘authcode‘).value="";
	}
  </script>

</body>
</html>

效果圖

技術分享




captcha-result.php(PHP判斷驗證碼是否正確):

<?php



//驗證驗證碼是否正確
if(isset($_REQUEST[‘authcode‘])){
	//開啟Session
	session_start();
	
	//strtolower()將字符串都轉換成小寫,將驗證碼設置為不區分大小寫型
	if(strtolower($_REQUEST[‘authcode‘]) == strtolower($_SESSION[‘authcode‘])){
		echo "輸入正確";
	}else{
		echo "輸入錯誤";
	}
	exit();
}



?>




所用到的函數原型:

<?php



//imagecreatetruecolor() 返回一個圖像標識符,代表了一幅大小為 x_size 和 y_size 的黑色圖像。
resource imagecreatetruecolor ( int $width , int $height );



//imagecolorallocate() 返回一個標識符,代表了由給定的 RGB 成分組成的顏色。red,green 和 blue 分別是所需要的顏色的紅,綠,藍成分。
int imagecolorallocate ( resource $image , int $red , int $green , int $blue );



//imagefill() 在 image 圖像的坐標 x,y(圖像左上角為 0, 0)處用 color 顏色執行區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。
bool imagefill ( resource $image , int $x , int $y , int $color );



//imagestring() 用 col 顏色將字符串 s 畫到 image 所代表的圖像的 x,y 坐標處(這是字符串左上角坐標,整幅圖像的左上角為 0,0)。如果 font 是 1,2,3,4 或 5,則使用內置字體。
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col );



//imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點。
bool imagesetpixel ( resource $image , int $x , int $y , int $color );



//imageline() 用 color 顏色在圖像 image 中從坐標 x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段。
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color );



//imagepng() 將 GD 圖像流(image)以 PNG 格式輸出到標準輸出(通常為瀏覽器),或者如果用 filename 給出了文件名則將其輸出到該文件。
bool imagepng ( resource $image [, string $filename ] );



//imagedestroy() 釋放與 image 關聯的內存。image 是由圖像創建函數返回的圖像標識符,例如 imagecreatetruecolor()。
bool imagedestroy ( resource $image );



?>




本文出自 “佟鑫的博客” 博客,請務必保留此出處http://tong707.blog.51cto.com/12527988/1955527

PHP實現驗證碼制作