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

php生成6位驗證碼

發布時間:2022-07-05 07:45:59

『壹』 thinkphp手機簡訊驗證碼怎麼處理

1、解壓附件到 ThinkPHPLibraryOrg 文件中
新建方法

publicfunctionsend(){
//初始化必填
$options['accountsid']='******';//填寫自己的
$options['token']='*****';//填寫自己的
//初始化$options必填
$ucpass=newOrgComUcpaas($options);

//隨機生成6位驗證碼
srand((double)microtime()*1000000);//createarandomnumberfeed.
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);
for($i=0;$i<6;$i++){
$randnum=rand(0,35);//10+26;
$authnum.=$list[$randnum];
}


//簡訊驗證碼(模板簡訊),默認以65個漢字(同65個英文)為一條(可容納字數受您應用名稱佔用字元影響),超過長度簡訊平台將會自動分割為多條發送。分割後的多條簡訊將按照具體佔用條數計費。

$appId="****";//填寫自己的
$to=$_POST['to'];
$templateId="1";
$param=$authnum;
$arr=$ucpass->templateSMS($appId,$to,$templateId,$param);
if(substr($arr,21,6)==000000){
//如果成功就,這里只是測試樣式,可根據自己的需求進行調節
echo"簡訊驗證碼已發送成功,請注意查收簡訊";

}else{
//如果不成功
echo"簡訊驗證碼發送失敗,請聯系客服";

}
}



前台頁面

<formid="form">
<inputtype="text"name="to"id="to"/>
<buttonid="submit">獲取驗證碼</button>
</form>
<scripttype="text/javascript">
$(function(){
$("#submit").click(function(){
vartourl=$("#form").attr("action");
$.post("__URL__/send",{to:$("#to").val()},function(data,textStatus){
alert(data);
});
})
})
</script>


測試時只能給注冊手機號和添加白名單手機號碼發送

『貳』 怎麼用原生php做驗證碼

<?php
class validateCode{
private $charset=''; //隨機因子
private $_len;
private $code; //驗證碼
private $codelen=4; //驗證碼長度
private $width=130; //畫布寬度
private $height=50; //畫布長度
private $img; //圖形資源句柄
private $font; //制定字體
private $fontsize=26;//字體大小
private $fontcolor; //字體顏色

//初始化構造函數
public function __construct(){
$this->font=dirname(__file__).'/validateFont.ttf';
//字體文件及路徑,__file__表示當前PHP文件絕對路徑,dirname表示這個PHP文件的絕對路徑的上一層
}

//生成隨機碼
private function createCode(){
$this->_len=strlen($this->charset)-1; //strlen表示計算字元串長度
for($i=1;$i<=$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$this->_len)];
}
}
//生成背景
private function createBG(){
$this->img=imagecreatetruecolor($this->width,$this->height);//創建圖像
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255)); //分配顏色函數(紅,綠,藍)
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); //矩形區域著色函數(要填充的圖像,x1,y1,x2,y2,要填充的顏色),x1,y1,x2,y2表示矩形的對角線
}

//生成文字
private function createFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]); //參數 size 為字形的尺寸;angle 為字型的角度,順時針計算,0 度為水平,也就是三點鍾的方向 (由左到右),90 度則為由下到上的文字;x,y 二參數為文字的坐標值 (原點為左上角);參數 col 為字的顏色;fontfile 為字型文件名稱,亦可是遠端的文件;text 當然就是字元串內容了。
}
}

//生成線條雪花
private function createLine(){
//線條
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}

//雪花
for($i=0;$i<40;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}

//輸出
private function outPut(){
header("content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}

//對外生成
public function doimg(){
$this->createCode();
$this->createBg();
$this->createFont();
$this->createLine();
$this->outPut();
}

//獲取驗證碼
public function getCode(){
return strtolower($this->code);
}
}
?>
字體自己去找,修改下路徑就行了

『叄』 php怎麼生成6位隨機數,php隨機數例子

通常情況下,當我們要生成一個隨機字元串時,總是先創建一個字元池,然後用一個循環和mt_rand()或rand()生成php隨機數,從字元池中隨機選取字元,最後拼湊出需要的長度。代碼如下:
function randomkeys($length)
{
$pattern = '
ABCDEFGHIJKLOMNOPQRSTUVWXYZ,./&l
t;>?;#:@~[]{}-_=+)(*&^%___FCKpd___0pound;"!'; //字元池
for($i=0;$i<$length;$i++)
{
$key .= $pattern{mt_rand(0,35)}; //生成php隨機數
}
return $key;
}
echo randomkeys(8);

這個php隨機函數能生成XC*=z~7L這樣的字元串,隨機!

『肆』 如何用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);//釋放圖片所佔內存
}

『伍』 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寫一個介面,隨機生成6位數字,然後以簡訊的形式發送到對應的手機號上。從來沒寫過介面不知道如何寫

簡訊介面需要你有對應的簡訊服務商才行,通常是第三方簡訊服務商,這種服務往往是收費的,收取的費用越多,每天可以發送的簡訊數量越多。
你需要向他們申請簡訊介面,申請成功後會有介面文檔,按照文檔中的規定發送相應的數據即可,通常發送的數據包含:需要發送簡訊的手機號、簡訊內容。
介面寫法通常會在文檔中有寫,甚至有的文檔會附帶範例以供開發者參考。

『捌』 Php中生成6位隨機數並顯示

Php中生成6位隨機數並顯示實現如下:

1、使用shuffle函數生成隨機數。

<?php

$arr=range(100000,999999);

shuffle($arr);

foreach($arras$values)

{

echo$values." ";//顯示隨機數

}

?>

2、使用array_unique函數生成隨機數。

<?php

$arr=array();

while(count($arr)<10)

{

$arr[]=rand(1,10);

$arr=array_unique($arr);

}

echoimplode(" ",$arr);

?>

(8)php生成6位驗證碼擴展閱讀:

PHP生成隨機數的兩種方法:

1、rand()函數:

//無參數:rand()函數用戶獲取隨機數。

echo rand() . " ";//得到一個不定位數的隨機數

//帶參數:rand($min,$max),$min表示從XX開始取值,$max表示最大隻能為XX。

echo rand(5, 15);//在5~15之間取一個數

注意:mt_rand() 用法跟rand()類似,但是mt_rand()的執行效率更高。

2、使用array_flip函數生成隨機數,可以去掉重復值。

閱讀全文

與php生成6位驗證碼相關的資料

熱點內容
自己購買雲主伺服器推薦 瀏覽:422
個人所得稅java 瀏覽:761
多餘的伺服器滑道還有什麼用 瀏覽:192
pdf劈開合並 瀏覽:28
不能修改的pdf 瀏覽:752
同城公眾源碼 瀏覽:489
一個伺服器2個埠怎麼映射 瀏覽:298
java字元串ascii碼 瀏覽:79
台灣雲伺服器怎麼租伺服器 瀏覽:475
旅遊手機網站源碼 瀏覽:332
android關聯表 瀏覽:946
安卓導航無聲音怎麼維修 瀏覽:333
app怎麼裝視頻 瀏覽:431
安卓系統下的軟體怎麼移到桌面 瀏覽:96
windows拷貝到linux 瀏覽:772
mdr軟體解壓和別人不一樣 瀏覽:904
單片機串列通信有什麼好處 瀏覽:340
游戲開發程序員書籍 瀏覽:860
pdf中圖片修改 瀏覽:288
匯編編譯後 瀏覽:491