A. php怎麼實現驗證碼的
驗證碼功能機制實現思路
常規的驗證碼實現:
a、產生一張png的圖片
b、為圖片設置背景色
c、設置字體顏色和樣式
d、產生4位數的隨機的驗證碼
e、把產生的每個字元調整旋轉角度和位置畫到png圖片上
f、加入噪點和干擾線防止注冊機器分析原圖片來惡意注冊
g、輸出圖片
h、釋放圖片所佔內存
i、將驗證碼保存到session或是資料庫
j、將和輸入的驗證碼進行對比
簡訊(郵箱)驗證碼機制:
a、產生4-6位數的隨機的驗證碼
b、把產生的每個字元保存到session或是資料庫
c、將驗證碼發送到用戶的手機(郵箱)
d、用戶在規定時間內進行輸入
e、將驗證碼從session或是資料庫中取出
f、將和輸入的驗證碼進行對比驗證
B. php中文驗證碼無法顯示
session_start();
$image=imagecreatetruecolor(200,60);//創建畫布
$color=imagecolorallocate($image,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));//隨機顏色
//$color=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$color);//填充顏色
//中文驗證碼
$fontface="simhei.ttf";//確保相同目錄下有該字體
$strdb=array('好','多','人','在','學','習');
for($i=0;$i<4;$i++){
$fontsizecolor=imagecolorallocate($image,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
$codex=iconv("GB2312","UTF-8",$strdb[mt_rand(0,5)]);//iconv不能轉數組取任意下標
imagettftext($image,mt_rand(20,24),mt_rand(-30,30),(40*$i+20),mt_rand(30,35),$fontsizecolor,$fontface,$codex);//如果用$code的話就生成1+2+3+4是個漢字的驗證碼了
}
//干擾點
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imagesetpixel($image,mt_rand(1,100),mt_rand(1,20),$pointcolor); //雪花
}
//干擾線
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imageline($image,mt_rand(1,99),mt_rand(1,99),mt_rand(1,99),mt_rand(1,99),$linecolor);
}
ob_clean();
header("Content-type:image/png");
imagepng($image);
imagedestroy($image);
C. 怎麼改dedecms verify.php驗證碼的字體高度和間距
arrayimagettftext
(resource$image
,float$size
,float$angle
,int$x
,int$y
橋大橡,int敏旁$color
,string$fontfile
,string$text
)
設仿明置size字體大小,默認14,可以設置大點的。
後台修改 圖片大小,注意inc_safe_config.php文件的寬高。
根據x,y設置字體坐標。
@imagettftext($im,$font_size,$c,$y_pos,19,$c_fontColor,$font_file,$rndstring[$i]);
改19的大小就行了。
D. Thinkphp驗證碼怎麼修改原始的字體大小
在生成驗證碼的方法里,設定一些參數運殲,如下所示
$verify = new \Think\Verify();
$verify->fontSize = 16; //字體大小
$verify->useNoise = true;//是否使用噪點
$verify->imageH = 30;//雀唯驗證碼高度
$verify->imageL = 140;//驗證頃悄培碼寬度
E. 如何用PHP生成驗證碼
PHP生成驗證碼的原理:使用PHP的GD庫,生成一張帶驗證碼的圖片,並將驗證碼保存在Session中。PHP生成驗證碼的大致流程有:
1、產生一張png的圖片;
2、為圖片設置背景色;
3、設置字體顏色和樣式;
4、產生4位數的隨機的驗證碼;
5、把產生的每個字元調整旋轉角度和位置畫到png圖片上;
6、加入噪點和干擾線防止注冊機器分析原圖片來惡意破解驗證碼;
7、輸出圖片;
8、釋放圖片所佔內存。
session_start();
getCode(4,60,20);
functiongetCode($num,$w,$h){
$code="";
for($i=0;$i<$num;$i++){
$code.=rand(0,9);
}
//4位驗證碼也可以用rand(1000,9999)直接生成
//將生成的驗證碼寫入session,備驗證時用
$_SESSION["helloweba_num"]=$code;
//創建圖片,定義顏色值
header("Content-type:image/PNG");
$im=imagecreate($w,$h);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
$bgcolor=imagecolorallocate($im,255,255,255);
//填充背景
imagefill($im,0,0,$gray);
//畫邊框
imagerectangle($im,0,0,$w-1,$h-1,$black);
//隨機繪制兩條虛線,起干擾作用
$style=array($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im,$style);
$y1=rand(0,$h);
$y2=rand(0,$h);
$y3=rand(0,$h);
$y4=rand(0,$h);
imageline($im,0,$y1,$w,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,$w,$y4,IMG_COLOR_STYLED);
//在畫布上隨機生成大量黑點,起干擾作用;
for($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,$w),rand(0,$h),$black);
}
//將數字隨機顯示在畫布上,字元的水平間距和位置都按一定波動范圍隨機生成
$strx=rand(3,8);
for($i=0;$i<$num;$i++){
$strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($code,$i,1),$black);
$strx+=rand(8,12);
}
imagepng($im);//輸出圖片
imagedestroy($im);//釋放圖片所佔內存
}
F. 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);
其他的頁面也是不需要修改的。
效果圖如下:
G. thinkphp 怎麼修改默認驗證碼
修改 ThinkPHP 默認驗證碼類的猛謹字體及大小
是否感覺ThinkPHP中的默認生成的驗猛笑證碼太小了?修改驗證碼的大小後,發現字體大小跟位置 沒變化,看上去感覺很不好,下面就來改改吧…
找到ThinkPHP框架目錄下的Extend/Library/ORG/Util/Image.class.php類文件打開,找到 buildImageVerify方法,大概在371行開始,用以下代碼替換掉以前的(改的東西並不多,只要 加上兩行代碼就可以了,但為了清楚那兩行代碼是加在哪貼出buildImageVerify的整個方法)
1 static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22,$verifyName='verify') {
2 import('ORG.Util.String');
3 $randval = String::randString($length, $mode);
4 session($verifyName, md5($randval));
5 $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
6 if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
7 $im = imagecreatetruecolor($width, $height);
8 } else {
9 $im = imagecreate($width, $height);
10 }
11 $r = Array(225, 255, 255, 223);
12 $g = Array(225, 236, 237, 255);
13 $b = Array(225, 236, 166, 125);
14 $key = mt_rand(0, 3);
15
16 $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(隨機)
17 $borderColor = imagecolorallocate($im, 100, 100, 100); //邊框色
18 imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
19 imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
20 $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
21 // 干擾枝知基
22 for ($i = 0; $i < 10; $i++) {
23 imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
24 }
25 for ($i = 0; $i < 25; $i++) {
26 imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
27 }
28 $fontPath=THINK_PATH."/Common/ERASDEMI.TTF"; //字體文件路徑,可自行更改,我放在ThinkPHP目錄下的Common目錄裡面
29 for ($i = 0; $i < $length; $i++) {
30 //imagestring($im, 5, $i * 18 + 5, mt_rand(1, 8), $randval{$i}, $stringColor); //這是默認的
31 imagettftext($im, 20, mt_rand(-30,30) , $i * 16 + 5, $height/1.4, $stringColor, $fontPath,$randval{$i}); //這個是新的,用imagettftext函數
32 }
33 Image::output($im, $type);
34 }