導航:首頁 > 編程語言 > php生成數字驗證碼

php生成數字驗證碼

發布時間:2022-12-31 22:15:04

❶ 請問php生成驗證碼的類怎麼寫

<?php
classCode{
//1.定義各個成員有寬、高、畫布、字數、類型、畫類型
private$width;//寬度
private$height;//高度
private$num;//驗證碼字數
private$imgType;//生成圖片類型
private$Type;//字串類型1,2,3三個選項1純數字2純小寫字母3大小寫數字混合
private$hb;//畫布
public$codestr;//驗證碼字串
publicfunction__construct($height=20,$num=4,$imgType="jpeg",$Type=1){
$this->width=$num*20;
$this->height=$height;
$this->num=$num;
$this->imgType=$imgType;
$this->Type=$Type;
$this->codestr=$this->codestr();
$this->zuhe();
}
//2.定義隨機獲取字元串函數
privatefunctioncodestr(){
switch($this->Type){
case1://類型為1獲取1-9隨機數
$str=implode("",array_rand(range(0,9),$this->num));
break;
case2://類型為2獲取a-z隨機小寫字母
$str=implode("",array_rand(array_flip(range(a,z)),$this->num));
break;
case3://類型為3獲取數字,小寫字母,大寫字母混合
for($i=0;$i<$this->num;$i++){
$m=rand(0,2);
switch($m){
case0:
$o=rand(48,57);
break;
case1:
$o=rand(65,90);
break;
case2:
$o=rand(97,122);
break;
}
$str.=sprintf("%c",$o);
}
break;
}

return$str;
}

//3.初始化畫布圖像資源
privatefunctionHb(){
$this->hb=imagecreatetruecolor($this->width,$this->height);
}
//4.生成背景顏色
privatefunctionBg(){
returnimagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));
}
//5.生成字體顏色
privatefunctionFont(){
returnimagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));
}
//6.填充背景顏色
privatefunctionBgColor(){
imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());
}
//7.干擾點
privatefunctionganrao(){
$sum=floor(($this->width)*($this->height)/3);
for($i=0;$i<$sum;$i++){
imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());
}
}
//8.隨機直線弧線
privatefunctionhuxian(){
for($i=0;$i<$this->num;$i++){
imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());
}
}
//9.寫字
privatefunctionxiezi(){
for($i=0;$i<$this->num;$i++){
$x=ceil($this->width/$this->num)*$i;
$y=rand(1,$this->height-15);
imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font());
}
}
//10.輸出
privatefunctionOutImg(){
$shuchu="image".$this->imgType;
$header="Content-type:image/".$this->imgType;
if(function_exists($shuchu)){
header($header);
$shuchu($this->hb);
}else{
exit("GD庫沒有此類圖像");
}
}
//11.拼裝
privatefunctionzuhe(){
$this->Hb();
$this->BgColor();
$this->ganrao();
$this->huxian();
$this->xiezi();
$this->OutImg();
}
publicfunctiongetCodeStr(){
return$this->codestr;
}
}

$a=newCode();
$a->getCodeStr();
?>

❷ 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寫個驗證碼

首先,當用戶打開頁面時隨機產生一個session,然後根據這個值生成驗證碼圖片。
第二,將驗證碼圖片顯示到表單上。
第三,當用戶提交時表單時,比較session里的值與表單中驗證碼的值進行比較。
簡單的實現過程:http://www.nowamagic.net/php/php_CheckCode.php
復雜的驗證碼圖片生成:http://www.admin5.com/article/20080314/75984.shtml

❹ php驗證碼怎麼實現

1.新建code.php驗證碼生成文件

在此之前必須打開php的GD庫,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分號。代碼如下:

<?php

session_start();

//生成驗證碼圖片

Header("Content-type: image/PNG");

$im = imagecreate(44,18);

$back = ImageColorAllocate($im, 245,245,245);

imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);

//生成4位數字

for($i=0;$i<4;$i++){

$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));

$authnum=rand(1,9);

$vcodes.=$authnum;

imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

}

for($i=0;$i<100;$i++) //加入干擾象素

{

$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

ImagePNG($im);

ImageDestroy($im);

$_SESSION['Checknum'] = $vcodes;

?>

2. 顯示驗證碼圖片

在需要顯示驗證碼的頁面中加入

<input type="text" name="passcode" >

<img src="code.php">

3.判斷並獲取驗證碼的值

驗證碼是通過第一步驟代碼中的$_SESSION['Checknum'] = $vcodes;賦的值,所以驗證碼的值存在$_SESSION['Checknum']當中。在驗證頁面,使用以下代碼,

...

session_start();//啟動會話

$code=$_POST["passcode"];

if( $code == $_SESSION["Checknum"])

{...}即可完成驗證碼登錄。

運行截圖:

望採納,謝謝

❺ PHP編寫的隨機生成4位數字的驗證碼,在代碼開頭加上<HTML>,<HEAD>,<TITLE>,就不能正常顯示。

在用php編寫繪圖的時候
這個php就相當於是 一個圖片文件 如後綴為jpg 的,你打開這類文件在上面html標記,哪當然果斷報錯!
php可以根據輸出的東西不同可變不同的文件
如 php讀入一個pdf文件直接輸出,那麼這個頁面就直接相當於是這個文件
沒一種文件都有自己的格式 html有他的格式 圖片有圖片的格式
所以這個是嚴重的錯誤,在輸出圖片的頁面是不能輸入任何字元的
就算在php標記之前打一個空格都會報錯
這就好像你電腦里的圖片,你用記事本打開,在最前面加個空格或其他神嗎的都會導致圖片無法打開

想要實現你的想法
只有這樣
文件a寫html標記
在裡面引用圖片 <img src="2.php" title="驗證碼" />

❻ PHP製作注冊頁面驗證碼

使用view-source:查看源碼會有錯誤提示,所以導致圖片無法正常輸出

❼ php如何使用隨機函數rand()生成一個數字驗證碼

參考這個
$code="";
//畫布
$image=imagecreatetruecolor(80, 25);
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));

for($i=0;$i<4;$i++){
$rand_color=imagecolorallocate($image, rand(0,155), rand(0,155), rand(0,155));
$code_tmp=dechex(rand(1,15));
$code.=$code_tmp;
imagestring($image, rand(4,5), rand($i*20,$i*20+20-6), rand(0,13),$code_tmp , $rand_color);
//干擾線
imageline($image, rand($i*20,$i*20+20), rand(0,25), rand($i*20,$i*20+20), rand(0,25), $rand_color);
imageline($image, rand($i*20,$i*20+20), rand(0,25), rand($i*20,$i*20+20), rand(0,25), $rand_color);
}

//保存
session_start();
$_SESSION['yzm']=$code;
session_write_close();

header("content-type:image/png");

imagepng($image);
imagedestroy($image);

❽ php中如何使用隨機函數rand()生成一個數字驗證碼

如果要生成四位數字的驗證碼,則可以用函數:

$srand = rand(1000,9999);

會生成在1000到9999之間的隨機數字,如果要生成更多位數的數字,可以更改最小、最大值。

❾ 怎樣製作PHP驗證碼

<?php
//驗證碼:文本類型為圖像
header("content-type:image/png");
define('TYPE',3);//1.字母 2.字母數字 3.數字 4.邏輯 5.漢字
session_start();
//創建畫布
$img = imagecreatetruecolor(90,33);
//創建顏色
//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$bgcolor = imagecolorallocate($img,255,255,255);
$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
//填充顏色到畫布
imagefill($img,0,0,$bgcolor);
//創建但像素的點為干擾項
//for($i=0;$i<100;$i++){
// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));
// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);
//}
//
////劃線
//$linecolor = imagecolorallocate($img,255,0,0);
//imageline($img,0,0,70,30,$linecolor);
//
////多邊形
// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );
// imagepolygon ( $img ,
// array (
// 5 , 5,
// 5, 15 ,
// 20,15,
// 20,5
// ),
// 4 ,
// $col_poly );
////弧線
//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );
//imagearc($img,35,15,30,30,0,360,$arcColor);

//創建驗證碼的內容
//#字母
$letter = range('A','Z');
$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];
//數字字母
$num = range(0,9);
$numberAndLetter = array_merge($letter,$num);
$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];
//#數字
$number = rand(1000,9999);
//#邏輯
$x = rand(1,9);
$y = rand(1,9);
$expression = $x."+".$y."=?";
$sum = $x+$y;
//#漢字
$CH = array('恭喜發財','財源滾滾','財源廣進','才高八斗','學富五車','抬頭見喜');
$chstr = $CH[rand(0,count($CH)-1)];
switch(TYPE){
case 1 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$letterStr);$_SESSION['code']=$letterStr;break;
case 2 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$nal);$_SESSION['code']=$nal;break;
case 3 : imagettftext($img,14,0,13,23,$textcolor,'MSYH.TTF',$number);$_SESSION['code']=$number;break;
case 4 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$expression);$_SESSION['code']=$sum;break;
case 5 : imagettftext($img,11,0,6,21,$textcolor,'MSYHBD.TTF',$chstr);$_SESSION['code']=$chstr;break;
}
//輸入圖像到瀏覽器
imagepng($img);
?>

❿ 如何用PHP生成驗證碼

打開php gd庫(你看下phpinfo是否打開,或者直接運行imagecreate,生成驗證碼)
然後利用session記錄驗證碼(頁面傳遞)

閱讀全文

與php生成數字驗證碼相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:144
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:736
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163