導航:首頁 > 編程語言 > php隨機數驗證碼

php隨機數驗證碼

發布時間:2022-06-01 19:29:02

php驗證碼判斷問題

沒明白你的意思, 驗證碼就是這樣, 生成的時候將驗證碼寫進session, 驗證的時候取出來的肯定是上次生成那個.

能詳細說一下你哪裡不明白么?

------------------------------

我想你是沒明白驗證碼的工作原理, 驗證碼多數情況是用來防止機器人自動發貼或注冊而採取的一種驗證手段, 在php中驗證碼多數用session來實現.

舉個例子讓你理解下流程.

1. 用戶訪問注冊頁面a.php
2. a.php輸出了一個驗證碼b.php,實際上等於用戶向b.php發送了一個請求.
3. b.php生成一個隨機數$a, 並將這個隨機數保存到session中, 然後將圖片顯示給用戶
4. 用戶填寫注冊表單並提交到c.php
5. c.php接受用戶注冊信息, 但在注冊之前要從session中取出隨機數$a和你提交上來的驗證碼做一下比較, 這個隨機數$a是在b.php那個請求中生成的, 也就是在a.php中看到的那個圖片. 比較如果相同那麼同意你進行申請, 如果不相同則返回錯誤信息並跳轉回a.php, 然後回到步驟1

這里我覺得你最應該了解的是session的作用,是用來在會話期間內在請求之間傳遞數據.

⑵ 怎麼讓php生成的驗證碼隨機數不重復

如果要生成四位數字的驗證碼,則可以用函數: $srand = rand(1000,9999); 會生成在1000到9999之間的隨機數字,如果要生成更多位數的數字,可以更改最孝最大值。

⑶ php驗證碼是保存在資料庫中還是直接在session中進行判斷的

我不明白你的意思,驗證碼時產生的驗證碼寫入會話,認證肯定取出,最後產生。

細節,你不明白是什麼?

------------------------------

我想以在大多數情況下,你不明白的工作原理驗證碼,驗證碼是用來防止機器人自動發貼或注冊身份驗證手段,達到最在PHP會話中使用驗證碼。

例如,你理解下面的過程。

用戶進入注冊頁面a.php中

2。 a.php中輸出驗證碼的b.php中,實際上等於用戶b.php中發送一個請求。

3 b.php中生成一個隨機數$ a,和隨機數被保存在會話中,畫面,然後顯示給用戶

用戶填寫登記表,並提交c.php

5的c.php接受用戶的注冊信息,但在登記前從會話中刪除隨機數驗證碼做一些比較,隨機數您提交b.php的要求,那就是,在生成a.php中看到圖片。同意你的應用程序,如果不相同則返回錯誤消息跳回a.php中,然後返回到相同的,所以第1步

我想你應該知道,本屆會議的作用,是用來傳輸數據之間的會話期間的要求。

⑷ 如何用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怎麼生成4位隨機純數字重復幾率要萬分之一以上!怎麼弄啊,急!

如果要生成四位數字的驗證碼,則可以用函數:$srand=rand(1000,9999);會生成在1000到9999之間的隨機數字,如果要生成位數的數字,可以更改最孝最大值。

⑺ 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隨機數驗證碼相關的資料

熱點內容
python列表求交集 瀏覽:872
解壓包如何轉音頻 瀏覽:447
機明自動編程軟體源碼 瀏覽:325
php埠號設置 瀏覽:541
phperegreplace 瀏覽:320
androidgridview翻頁 瀏覽:537
ssh協議編程 瀏覽:634
如何開我的世界電腦伺服器地址 瀏覽:861
玄關pdf 瀏覽:609
程序員學習論壇 瀏覽:940
程序員的毒雞湯怎麼做 瀏覽:548
安卓怎麼降級軟體到手機 瀏覽:281
雲與伺服器入門書籍推薦產品 瀏覽:636
delphi編程助手 瀏覽:762
電腦遇到伺服器問題怎麼辦 瀏覽:515
加工中心編程結束方法 瀏覽:296
了解什麼是web伺服器 瀏覽:140
面向對象的編程的基本特徵 瀏覽:718
php定時執行任務linux 瀏覽:787
php數組中刪除元素 瀏覽:725