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

php生成4位驗證碼

發布時間:2023-07-18 00:39:53

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中如何使用隨機函數rand()生成一個數字驗證碼

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

$srand = rand(1000,9999);

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

⑶ php怎麼實現驗證碼的

驗證碼功能機制實現思路

  1. 常規的驗證碼實現:

    a、產生一張png的圖片


    b、為圖片設置背景色


    c、設置字體顏色和樣式


    d、產生4位數的隨機的驗證碼


    e、把產生的每個字元調整旋轉角度和位置畫到png圖片上


    f、加入噪點和干擾線防止注冊機器分析原圖片來惡意注冊


    g、輸出圖片


    h、釋放圖片所佔內存


    i、將驗證碼保存到session或是資料庫


    j、將和輸入的驗證碼進行對比

  2. 簡訊(郵箱)驗證碼機制:

    a、產生4-6位數的隨機的驗證碼


    b、把產生的每個字元保存到session或是資料庫


    c、將驗證碼發送到用戶的手機(郵箱)


    d、用戶在規定時間內進行輸入


    e、將驗證碼從session或是資料庫中取出


    f、將和輸入的驗證碼進行對比驗證

⑷ 我的php代碼中登陸界面加一個驗證碼,如何實現

php登陸頁面+驗證碼的實現,參考如下:

1、首先新建一個php站點;

⑸ 請問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的簡訊驗證常式,

<?php

/**

*

* User: shikiliu

* Date: 13-7-11

*/

class TelephoneCheck

{



/**

* 取得某個用戶某次活動的手機驗證碼

* @param $uin 用戶ID 小於10000系統保留

* @param $actId 活動ID 小於1000系統保留

* @param $telephone 用戶手機號

* @return bool|int 4位數的驗證碼

*/

public function getTelephoneCode($uin, $actId, $telephone)

{


if ($uin < 10000 || $actId < 1000 || empty($telephone)) {

return false;

}


$time = time();


$timeFeature = hexdec(substr(md5($time), 0, 3)) & 0x1F1;


$telephoneFeature = hexdec(substr(md5($telephone), 8, 4));


$actIdFeature = hexdec(substr(md5($actId), 16, 4));


$uinFeature = hexdec(substr(md5($uin), 24, 4));


$sumFeature = $telephoneFeature + $actIdFeature + $uinFeature;


$sumFeature = $sumFeature % 10000;


if ($sumFeature < 1000) {

$sumFeature = 5145;

}


$result = $sumFeature | $timeFeature;


return $result;

}



/**

* 驗證用戶的手機驗證碼

* @param $uin 用戶ID 小於10000系統保留

* @param $actId 活動ID 小於1000系統保留

* @param $telephone 用戶手機號

* @param $code getTelephoneCode生成的驗證碼

* @return bool 是否正確

*/

public function checkTelephoneCode($uin, $actId, $telephone, $code)

{


if ($uin < 10000 || $actId < 1000 || empty($telephone) || empty($code)) {

return false;

}


$telephoneFeature = hexdec(substr(md5($telephone), 8, 4));


$actIdFeature = hexdec(substr(md5($actId), 16, 4));


$uinFeature = hexdec(substr(md5($uin), 24, 4));


$sumFeature = $telephoneFeature + $actIdFeature + $uinFeature;


$sumFeature = $sumFeature % 10000;


if ($sumFeature < 1000) {

$sumFeature = 5145;

}


$sumFeature = $sumFeature & 0xE0E;


$code = $code & 0xE0E;


if ($sumFeature == $code) {

return true;

}

return false;

}

}



$actId = 10001;

$telephone = 13797025562;

$uin = 514540767;



$telCode = new TelephoneCheck();



$code = $telCode->getTelephoneCode($uin, $actId, $telephone);


var_mp($code);


var_mp($telCode->checkTelephoneCode($uin, $actId, $telephone, $code));


var_mp($telCode->checkTelephoneCode($uin, $actId, $telephone, $code+10));

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

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

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

閱讀全文

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

熱點內容
php和類名相同的方法 瀏覽:360
香港台灣dns伺服器地址列表 瀏覽:537
大同app怎麼樣 瀏覽:438
php去掉特殊字元 瀏覽:387
androidapi中文合集 瀏覽:658
win7下安裝linux虛擬機 瀏覽:838
雲主機用別的伺服器嗎 瀏覽:922
黑馬買入指標源碼副圖 瀏覽:962
微軟為什麼會把伺服器放在水底 瀏覽:257
php截取字元串中文 瀏覽:21
虛擬機和編譯軟體哪個好 瀏覽:750
存儲伺服器為什麼比通用伺服器難 瀏覽:373
用php列印出前一天的時間 瀏覽:369
2010編譯方法 瀏覽:239
華為哪裡查看隱藏app 瀏覽:889
linux網卡重置 瀏覽:830
框架柱低於四米箍筋全高加密 瀏覽:694
米二如何安卓版本升級到高安卓版 瀏覽:783
安卓手機數據慢怎麼辦 瀏覽:727
雙底買賣指標公式源碼無未來函數 瀏覽:685