"驗證碼用于屏蔽機器寫入!!!"
目標:在底圖上顯示隨機內容(如數字)
方法:init imagecolorallocate ( resource $image , int $red , int $green , int $blue)
bool imagestring ( resource $image , int $font , int $x , int $y , string $s ,
int $col)
注意事項:控制好字體與分布,避免字體重疊或顯示不全
目標:為驗證碼增加干擾元素,干擾的點或線(兩點確定一條線)
方法:bool imagesetpixel(resource $image,int $x,int $y,int $color)
bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color)
注意事項:干擾信息一定控制好顏色,避免“喧賓奪主”
captcha.php:
<?php
//生成一張背景圖片
$image = imagecreatetruecolor(100,30);//創(chuàng)建一個寬100高30的區(qū)域
$bgcolor = imagecolorallocate($image,255,255,255);//#ffffff
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);//設置x、y軸區(qū)間
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//為底圖添加干擾點
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, 29), $pointcolor);
}
//為底圖添加干擾線
for( $i = 0; $i < 3; $i++) {
$linecolor = imagecolorallocate( $image, rand(80,220), rand(80,220));
imageline( $image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), linecolor);
}
header("Content-type: text/html; charset=utf-8");//亂碼問題
header('content-type:image/png');
imagepng($image);
//end
imagedestory($image);
目標:讓圖片上的驗證碼內容顯示為字母,或數字、字母混合體
方法:int rand ( int $min, int $max )
mixed array_rand ( array $input [, int $num_req = 1] )
注意事項:N/A
for( $i = 0; $i < 4; $i++) {
$fontsize = 6;
$fontcolor = imagecolorallocate( $image, rand(0,120), rand(0,120),rand(0,120));
$data = 'abcdefghijklmnopqrstuvwxyz123456789';
$fontcontent = substr( $data,rand(0,strlen($data)-1), 1 );
$x=( $i * 100 / 4 )+rand(5,10);//設置x、y軸區(qū)間
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
目標:再服務器端記錄驗證碼信息,便于用戶輸入后做校驗
方法:bool session_start ( void )
注意事項:1. session_start()必須處于腳本最頂部
2. 多服務器情況,需要考慮幾種管理session信息
<?php
session_start();
$captch_code = '';
for( $i = 0; $i < 4; $i++) {
$fontsize = 6;
$fontcolor = imagecolorallocate( $image, rand(0,120), rand(0,120),rand(0,120));
$data = 'abcdefghijklmnopqrstuvwxyz123456789';
$fontcontent = substr( $data,rand(0,strlen($data)-1), 1 );
$x=( $i * 100 / 4 )+rand(5,10);//設置x、y軸區(qū)間
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//保存到session中指定字段里
$_SESSION['authcode'] = $captch_code;
目標:將已生成的驗證碼提供給用戶,并校驗用戶驗證碼的正確性
方法:HTML<form>表單基礎
注意事項:N/A
<?php
if(isset($_REQUEST['authcode']))
{
session_start();
//strtolower 轉化為小寫
if (strtolower($_REQUEST['authcode'])==$_SESSION['authcode'])
{
header('Content-type: text/html; charset=UTF8');
echo '<font color="#0000CC">輸入正確</font>';
}
else{
header('Content-type: text/html; charset=UTF8');
echo '<font color="#CC0000"><b>輸入錯誤</b></font>';
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>確認驗證</title>
</head>
<body>
<form method="post" action="./form.php">
<p>驗證碼圖片:<img id="captcha_img" border="1" src="./captcha.php?r=<?php echo rand();?>" width="100" height="30">
<a href="javascript:void(0)" onClick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">換一個?</a>
</p>
<p>請輸入圖片的內容:<input type="text" name="authcode" value=""/></p>
<p><input type="submit" value="提交" style="padding:6px 20px;"></p>
</form>
</body>
</html>
1. 增加可點擊的“換一個”文案
2. 用js選取器選取驗證碼圖片
3. 用js修改驗證碼圖片地址(改src)