代碼細(xì)節(jié)問(wèn)題,自己做筆記寫(xiě)的,有點(diǎn)問(wèn)題。
captcha.php代碼如下:
session_start();
$image = imagecreatetruecolor( 100, 30 );// 新建一個(gè)真彩色圖像
/**
* imagecolorallocate — 為一幅圖像分配顏色。
* 說(shuō)明:int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
*/
$bgcolor = imagecolorallocate($image,255,255,255);
/**
* imagefill — 區(qū)域填充。
* 說(shuō)明:bool imagefill ( resource $image , int $x , int $y , int $color )
* 在 image 圖像的坐標(biāo) x,y(圖像左上角為 0, 0)處用 color 顏色執(zhí)行區(qū)域填充(即與 x, y 點(diǎn)顏色相同且相鄰的點(diǎn)都會(huì)被填充)。
*/
imagefill( $image, 0, 0, $bgcolor );
/**
* 數(shù)字驗(yàn)證碼的實(shí)現(xiàn):主要用到 imagestring 這個(gè)函數(shù);$i表示驗(yàn)證碼個(gè)數(shù)
*/
//for($i=0; $i<4; $i++){
// ? ?$fontsize = 5;
// ? ?$fontcolor = imagecolorallocate( $image, rand(0,125), rand(0,125), rand(0,125) );
// ? ?$fontcontent = rand(0,9);
// ? ?$fontx = ($i*100/4) + rand(5,10);
// ? ?$fonty = rand(5,10);
//
// ? ?/**
// ? ? * ?bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
// ? ? * ?用 col 顏色將字符串 s 畫(huà)到 image 所代表的圖像的 x,y 坐標(biāo)處(這是字符串左上角坐標(biāo),整幅圖像的左上角為 0,0)。
// ? ? * ?如果 font 是 1,2,3,4 或 5,則使用內(nèi)置字體
// ? ? */
// ? ?imagestring($image,$fontsize,$fontx,$fonty,$fontcontent,$fontcolor);
//}
/**
* 字母驗(yàn)證碼的實(shí)現(xiàn):主要用到 imagestring 這個(gè)函數(shù);$i表示驗(yàn)證碼個(gè)數(shù)
*/
$captcha_code = '';
for($i=0;$i<4;$i++){
? ?$fontsize = 6;
? ?$fontcolor = imagecolorallocate( $image, rand(0,125), rand(0,125), rand(0,125) );
? ?$date = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789';
? ?$fontcontent = substr($date,rand(0,strlen($date)-1),1);
? ?$captcha_code .= $fontcontent;
? ?$fontx = ($i*100/4) + rand(5,10);
? ?$fonty = rand(5,10);
? ?/**
? ? * ?bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
? ? * ?用 col 顏色將字符串 s 畫(huà)到 image 所代表的圖像的 x,y 坐標(biāo)處(這是字符串左上角坐標(biāo),整幅圖像的左上角為 0,0)。
? ? * ?如果 font 是 1,2,3,4 或 5,則使用內(nèi)置字體
? ? */
? ?imagestring($image,$fontsize,$fontx,$fonty,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $captcha_code;
/**
* 驗(yàn)證碼點(diǎn)干擾元素的實(shí)現(xiàn):主要用到 imagesetpixel 函數(shù);$i 表示干擾元素個(gè)數(shù)
*/
for($i=0;$i<200;$i++){
? ?$pointcolor = imagecolorallocate( $image, rand(50,200), rand(50,200), rand(50,200) );
? ?/**
? ? * bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
? ? * 用 col 顏色將字符串 s 畫(huà)到 image 所代表的圖像的 x,y 坐標(biāo)處(這是字符串左上角坐標(biāo),整幅圖像的左上角為 0,0)。
? ? * 如果 font 是 1,2,3,4 或 5,則使用內(nèi)置字體。
? ? */
? ?imagesetpixel( $image,rand(1,99),rand(1,99),$pointcolor);
}
/**
* 驗(yàn)證碼線干擾元素的實(shí)現(xiàn):主要用到 imageline 函數(shù):$i表示線的條數(shù)。
*/
for($i=0;$i<4;$i++){
? ?$linecolor = imagecolorallocate( $image, rand(80,220), rand(80,220), rand(80,220) );
? ?/**
? ? * ?bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
? ? * ?用 color 顏色在圖像 image 中從坐標(biāo) x1,y1 到 x2,y2(圖像左上角為 0, 0)畫(huà)一條線段。
? ? */
? ?imageline( $image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $linecolor);
}
header('content-type:image/png');
imagepng( $image );//imagepng — 以 PNG 格式將圖像輸出到瀏覽器或文件
imagedestroy( $image );//imagedestroy — 銷毀一圖像
from.php代碼如下:
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/3/18
* Time: 9:34
*/
header("Content-type:text/html;charset=utf-8");
if(isset($_REQUEST["authcode"])){
? ?print_r($_REQUEST["authcode"]);
session_start();
print_r($_SESSION["authcode"]);
if( strtolower($_REQUEST["authcode"]) == $_SESSION["authcode"] ){
echo "1";
}else{
echo "0";
}
// ? ?if ($_REQUEST["authcode"] == $_SESSION["authcode"])
// ? ?{
// ? ? ? ?header("Content-type: text/html; charset=UTF8");
// ? ? ? ?echo "<h5 color="#0000CC">輸入正確</h5>";
// ? ?}else{
// ? ? ? ?header("Content-type: text/html; charset=UTF8");
// ? ? ? ?echo "<h5 color="#CC0000">輸入錯(cuò)誤</h5>";
// ? ? ? ?echo $_REQUEST["authcode"];
// ? ?}
exit;
}
?>
<!doctype html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>確認(rèn)驗(yàn)證碼</title>
</head>
<body>
<form method="post" action="./from.php">
? ?<p>驗(yàn)證碼圖片:<img border="1" src="./captcha.php/r=<?php echo rand();?>" width=100,height=30";</p>
? ?<p>請(qǐng)輸入圖片中的內(nèi)容:<input type="text" name="authcode" value=""></p>
? ?<p><input type="submit" value="提交" style="padding:6px 20px;"></p>
</form>
</body>
</html>
具體不清楚哪兒有問(wèn)題,驗(yàn)證信息的時(shí)候,兩個(gè)數(shù)據(jù)都是一樣的, 可是輸出去卻總是“0”,當(dāng)局者迷,旁觀者清。求大神幫忙看下~謝謝!
2017-03-21
曉得是什么原因了~