Ⅰ php如何實現驗證碼許昌鯉魚IT計算機電腦軟體編程培訓中心
驗證碼在表單實現越來越多了,但是用js的寫的驗證碼,總覺得不方便,所以學習了下php實現的驗證碼。好吧,其實是沒有事情干,但是又不想浪費時間,所以學習了下php實現驗證碼。正所謂,技多不壓身。而且,也可以封裝成一個函數,以後使用的時候也是很方便的,當然現在未封裝。
現在來說說簡單的純數字驗證碼吧。
如果是初學者,建議按照我代碼的注釋 //數字 一步步來。最簡單的方法,還是把整個代碼復制走了。
新建一個captcha.php:
php //10>設置session,必須處於腳本最頂部
session_start(); $image = imagecreatetruecolor(100, 30); //1>設置驗證碼圖片大小的函數
//5>設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor); //10>設置變數
$captcha_code = ""; //7>生成隨機數字
for($i=0;$i<4;$i++){ //設置字體大小
$fontsize = 6;
//設置字體顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//設置數字
$fontcontent = rand(0,9); //10>.=連續定義變數
$captcha_code .= $fontcontent;
//設置坐標
$x = ($i*100/4)+rand(5,10); $y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
} //10>存到session
$_SESSION['authcode'] = $captcha_code; //8>增加干擾元素,設置雪花點
for($i=0;$i<200;$i++){ //設置點的顏色,50-200顏色比數字淺,不幹擾閱讀
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 畫一個單一像素
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
} //9>增加干擾元素,設置橫線
for($i=0;$i<4;$i++){ //設置線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設置線,兩點一線
imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
} //2>設置頭部,image/png
header('Content-Type: image/png'); //3>imagepng() 建立png圖形函數
imagepng($image); //4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);
接著就是靜態頁的代碼了:index.html
doctype html><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>確認驗證碼title>
head>
<body>
<form method="post" action="./form.php">
<p>驗證碼: <img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?>' style="width:100px; height:30px" /> <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 5px;'/>p>
body>html>
從index.html可以看到,提交的表單是到form.php的,所以還要有一個判斷的form.php代碼:
php header("Content-Type:text/html;charset=utf-8"); //設置頭部信息
//isset()檢測變數是否設置
if(isset($_REQUEST['authcode'])){ session_start(); //strtolower()小寫函數
if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){ //跳轉頁面
echo "<script language=\"javascript\">"; echo "document.location=\"./form.php\""; echo "</script>";
}else{ //提示以及跳轉頁面
echo "<script language=\"javascript\">"; echo "alert('輸入錯誤!');"; echo "document.location=\"./form.php\""; echo "</script>";
} exit();
}
那麼,純數字的實現了,數字加英文的也應該不難了。要修改的代碼 只是在 captcha.php 將 //7>生成隨機數字 修改成 //7>生成隨機的字母和數字,如果你真的很可愛的就修改這幾個字就認為可以實現的話,那麼祝賀你,你永遠保持快樂。腦殘兒童歡樂多。
廢話不多說了,拉代碼吧。
php //10>設置session,必須處於腳本最頂部
session_start(); $image = imagecreatetruecolor(100, 30); //1>設置驗證碼圖片大小的函數
//5>設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor); //10>設置變數
$captcha_code = ""; //7>生成隨機的字母和數字
for($i=0;$i<4;$i++){ //設置字體大小
$fontsize = 8;
//設置字體顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//設置需要隨機取的值,去掉容易出錯的值如0和o
$data =''; //取出值,字元串截取方法 strlen獲取字元串長度
$fontcontent = substr($data, rand(0,strlen($data)),1); //10>.=連續定義變數
$captcha_code .= $fontcontent;
//設置坐標
$x = ($i*100/4)+rand(5,10); $y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
} //10>存到session
$_SESSION['authcode'] = $captcha_code; //8>增加干擾元素,設置雪花點
for($i=0;$i<200;$i++){ //設置點的顏色,50-200顏色比數字淺,不幹擾閱讀
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 畫一個單一像素
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
} //9>增加干擾元素,設置橫線
for($i=0;$i<4;$i++){ //設置線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設置線,兩點一線
imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
} //2>設置頭部,image/png
header('Content-Type: image/png'); //3>imagepng() 建立png圖形函數
imagepng($image); //4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);
其他的兩個頁面,不許要修改。
一般而言,現在就已經夠用了。但是就像動漫一樣,總會有番外。
那麼,我們來個漢字的番外吧。其實我也准備將漢字的驗證碼放到我的畢業設計裡面,雖然現在很流行滑動驗證碼,但是本人畢竟不是專門學習js的。
而且,還可以和答辯的老師說,我們驗證碼不需要素材,連圖片也是生成的,用自己的知識裝13,也沒有設么的。
php //11>設置session,必須處於腳本最頂部
session_start(); //1>設置驗證碼圖片大小的函數
$image = imagecreatetruecolor(200, 60);
//5>設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor); //7>設置ttf字體
$fontface = 'FZYTK.TTF'; //7>設置字型檔,實現簡單的數字儲備
$str='天地不仁以萬物為芻狗聖人不仁以百姓為芻狗這句經常出現在控訴暴君暴政上地殘暴不仁把萬物都當成低賤的豬狗來看待而那些高高在上的所謂聖人們也沒兩樣還不是把我們老百姓也當成豬狗不如的東西但實在正取的解讀是地不情感用事對萬物一視同仁聖人不情感用事對百姓一視同仁執子之手與子偕老當男女主人公含情脈脈看著對方說了句執子之手與子偕老女方淚眼朦朧含羞地回一句討厭啦這樣的情節我們是不是見過很多但是我們來看看這句的原句死生契闊與子成說執子之手與子偕老於嗟闊兮不我活兮於嗟洵兮不我信兮意思是說戰士之間的約定說要一起死現在和我約定的人都走了我怎麼活啊赤裸裸的兄弟江湖戰友友誼啊形容好基友的基情比男女之間的愛情要合適很多吧'; //str_split()切割字元串為一個數組,一個中文在utf_8為3個字元
$strdb = str_split($str,3);
//>11
$captcha_code = ''; //8>生成隨機的漢子
for($i=0;$i<4;$i++){ //設置字體顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//隨機選取中文
$in = rand(0,count($strdb)); $cn = $strdb[$in]; //將中文記錄到將保存到session的字元串中
$captcha_code .= $cn; /*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,
string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐標,顏色,字體路徑,文本字元串
mt_rand()生成更好的隨機數,比rand()快四倍*/
imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);
} //11>存到session
$_SESSION['authcode'] = $captcha_code; //9>增加干擾元素,設置點
for($i=0;$i<200;$i++){ //設置點的顏色,50-200顏色比數字淺,不幹擾閱讀
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 畫一個單一像素
imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);
} //10>增加干擾元素,設置線
for($i=0;$i<4;$i++){ //設置線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設置線,兩點一線
imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);
} //2>設置頭部,image/png
header('Content-Type: image/png'); //3>imagepng() 建立png圖形函數
imagepng($image); //4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);
其他的頁面也是不需要修改的。
效果圖如下:
Ⅱ 像這樣的驗證碼有沒有辦法用 PHP 識別
一般沒辦法 只能用一些辦法來搞 比如curl 把圖片存下來 然後用txt寸你看到的驗證碼 然後PHP讀取txt內容 或者你用一些打碼平台!
Ⅲ php圖片驗證碼實現
可以用php的GD庫做
//隨機生成驗證碼
class randomString
{
function createRandomStr($strLen)
{
list($usec, $sec) = explode(' ', microtime());
(float) $sec + ((float) $usec * 100000);
$number = '';
$number_len = $strLen;
$stuff = '';//附加碼顯示範圍ABCDEFGHIJKLMNOPQRSTUVWXYZ
$stuff_len = strlen($stuff) - 1;
for ($i = 0; $i < $number_len; $i++) {
$number .= substr($stuff, mt_rand(0, $stuff_len), 1);
}
return $number;
}
}
通過ZD庫將驗證碼變成圖片
$number = $createStr->createRandomStr('4');//驗證碼的位數
$number_len = strlen($number);
$_SESSION["VERIFY_CODE"] = $number;
// 生成驗證碼圖片
$img_width = 60;
$img_height = 20;
$img = imageCreate($img_width, $img_height);
ImageColorAllocate($img, 0x6C, 0x74, 0x70);
$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);
$ix = 6;
$iy = 2;
for ($i = 0; $i < $number_len; $i++) {
imageString($img, 5, $ix, $iy, $number[$i], $white);
$ix += 14;
}
for($i=0;$i<200;$i++) //加入干擾象素
{
$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);
}
// 輸出圖片
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($img);
imagedestroy($img);
Ⅳ PHP寫的圖形驗證碼亂碼
我吧我的給你用吧
class wxhVerify {
static function imageVerify($length=4, $mode=1, $width=48, $height=22, $verifyName='verify') {
$randval = randomString($length, $mode);
$_SESSION[$verifyName] = md5($randval);
$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
$im = @imagecreatetruecolor($width, $height);
$r = Array(225, 255, 255, 223);
$g = Array(225, 236, 237, 255);
$b = Array(225, 236, 166, 125);
$key = mt_rand(0, 3);
//隨機背景色
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);
//邊框色
$borderColor = imagecolorallocate($im, 100, 100, 100);
//點顏色
$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
//干擾
for ($i = 0; $i < 10; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 25; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}
for ($i = 0; $i < $length; $i++) {
imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
exit;
}
}
//生成隨機字元串
function randomString($len=6, $type=1, $addChars='') {
switch ($type) {
case 0:
$chars = '' . $addChars;
break;
case 1:
$chars = str_repeat('0123456789', 3);
break;
case 2:
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
break;
case 3:
$chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
break;
default :
//默認去掉了容易混淆的字元oOLl和數字01,要增加請使用addChars參數
$chars = '' . $addChars;
break;
}
if ($len > 10) {
//位數過長重復字元串一定次數
$chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);
}
$chars = str_shuffle($chars);
return substr($chars, 0, $len);
}
調用 wxhVerify::imageVerify();
Ⅳ php怎麼識別驗證碼
使用圖片驗證碼,一般注冊機很難識別圖片上的字元。 圖片驗證碼原理:在注冊表單頁上,添加一個表單項,用於提交用戶看到的圖片驗證碼,在適合位置放一個
Ⅵ 求 PHP 圖片驗證碼類 給出詳細調用方法 謝謝!!!
[code.php]
<?php
/**
*驗證碼圖片
*/
session_start();
Header("Content-type:image/gif");
/*
*初始化
*/
$border=0;//是否要邊框1要:0不要
$how=4;//驗證碼位數
$w=$how*15;//圖片寬度
$h=20;//圖片高度
$fontsize=10;//字體大小
$alpha="abcdefghijkmnpqrstuvwxyz";//驗證碼內容1:字母
$number="23456789";//驗證碼內容2:數字
$randcode="";//驗證碼字元串初始化
srand((double)microtime()*1000000);//初始化隨機數種子
$im=ImageCreate($w,$h);//創建驗證圖片
/*
*繪制基本框架
*/
$bgcolor=ImageColorAllocate($im,255,255,255);//設置背景顏色
ImageFill($im,0,0,$bgcolor);//填充背景色
if($border)
{
$black=ImageColorAllocate($im,0,0,0);//設置邊框顏色
ImageRectangle($im,0,0,$w-1,$h-1,$black);//繪制邊框
}
/*
*逐位產生隨機字元
*/
for($i=0;$i<$how;$i++)
{
$alpha_or_number=mt_rand(0,1);//字母還是數字
$str=$alpha_or_number?$alpha:$number;
$which=mt_rand(0,strlen($str)-1);//取哪個字元
$code=substr($str,$which,1);//取字元
$j=!$i?4:$j+15;//繪字元位置
$color3=ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));//字元隨即顏色
ImageChar($im,$fontsize,$j,3,$code,$color3);//繪字元
$randcode.=$code;//逐位加入驗證碼字元串
}
/*
*如果需要添加干擾就將注釋去掉
*
*以下for()循環為繪背景干擾線代碼
*/
/*+-------------------------------繪背景干擾線開始--------------------------------------------+*/
for($i=0;$i<5;$i++)//繪背景干擾線
{
$color1=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干擾線顏色
ImageArc($im,mt_rand(-5,$w),mt_rand(-5,$h),mt_rand(20,300),mt_rand(20,200),55,44,$color1);//干擾線
}
/*+-------------------------------繪背景干擾線結束--------------------------------------+*/
/*
*如果需要添加干擾就將注釋去掉
*
*以下for()循環為繪背景干擾點代碼
*/
/*+--------------------------------繪背景干擾點開始------------------------------------------+*/
/*
for($i=0;$i<$how*40;$i++)//繪背景干擾點
{
$color2=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干擾點顏色
ImageSetPixel($im,mt_rand(0,$w),mt_rand(0,$h),$color2);//干擾點
}
*/
/*+--------------------------------繪背景干擾點結束------------------------------------------+*/
//把驗證碼字元串寫入session方便提交登錄信息時檢驗驗證碼是否正確例如:$_POST['randcode']=$_SESSION['randcode']
$_SESSION['randcode']=$randcode;
/*繪圖結束*/
Imagegif($im);
ImageDestroy($im);
/*繪圖結束*/
?>
[調用方法]
<SCRIPTLANGUAGE="JavaScript">
<!--
functionreloadcode(){
vard=newDate();
document.getElementById('safecode').src="/code.php?t="+d.toTimeString()
}
//-->
</SCRIPT>
驗證碼:<inputname="chknumber"type="text"maxlength="4"class="chknumber_input"/><imgsrc='code.php'id="safecode"onclick="reloadcode()"title="看不清楚?點擊切換!"></img>
Ⅶ php的圖片驗證碼代碼
這個是phpcms的驗證碼,經過十幾萬個網站經驗的,非常好用
<?php
session_start();
$enablegd = 1;
//判斷圖像處理函數是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}
ob_clean(); //清理緩沖
if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //獲取$consts中的一個隨機數
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //獲取$vowels中的一個隨機數
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //顯示4個字元
$imageX = strlen($radomstring)*8; //圖像的寬
$imageY = 20; //圖像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一個真彩色圖像
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$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))
);
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配顏色並說明透明度
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中間背景
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中間背景2
//與左上角的顏色相同的都會被填充
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//往圖像上寫入文字
imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);
//畫邊框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
//畫一些隨機出現的點
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//畫隨機出現的線
for ($x=0; $x<9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //畫線
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //畫橢圓
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
}
else
{
$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');
if(!is_array($files)) die('請檢查文件目錄完整性:/images/checkcode/');
$checkcodefile = $files[rand(0, count($files)-1)]; //隨機其中一個文件
$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //獲得文件名
header("content-type:image/jpeg\r\n");
include $checkcodefile;
}
?>
Ⅷ 如何用PHP寫圖片驗證碼驗證代碼
你去php100看看視頻教程吧,踏踏實實的看完了,你的問題也就解決了