1. 怎麼讓php生成的驗證碼隨機數不重復
如果要生成四位數字的驗證碼,則可以用函數: $srand = rand(1000,9999); 會生成在1000到9999之間的隨機數字,如果要生成更多位數的數字,可以更改最孝最大值。
2. 如何用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);//釋放圖片所佔內存
}
3. 驗證碼怎麼用php實現
<?php
/*
* Filename: authpage.php
*/
srand((double)microtime()*1000000);
//驗證用戶輸入是否和驗證碼一致
if(isset($HTTP_POST_VARS['authinput']))
{
if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0)
echo "驗證成功!";
else
echo "驗證失敗!";
}
//生成新的四位整數驗證碼
while(($authnum=rand()%10000)<1000);
?>
<form action=authpage.php method=post>
<table>
請輸入驗證碼:<input type=text name=authinput style="width:
80px"><br>
<input type=submit name="驗證" value="提交驗證碼">
<input type=hidden name=authnum value=<? echo $authnum; ?>>
<img src=authimg.php?authnum=<? echo $authnum; ?>>
</table>
</form>
代碼二:
<?php
/*
* Filename: authimg.php
* Author: hutuworm
* Date: 2003-04-28
* @Copyleft hutuworm.org
*/
//生成驗證碼圖片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(58,28);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
//將四位整數驗證碼繪入圖片
imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);
for($i=0;$i<50;$i++) //加入干擾象素
{
imagesetpixel($im, rand()%70 , rand()%30 , $black);
}
ImagePNG($im);
ImageDestroy($im);
?>
4. 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的作用,是用來在會話期間內在請求之間傳遞數據.
5. php驗證碼判斷
session_start();
$str_number = trim($_POST['number']);
if(strtolower($_SESSION['rand'])==strtolower($str_number )){
echo "驗證碼正確";
}else{
echo "驗證碼不正確";
}
最好加strtolower函數轉換下大小寫,這樣子,用戶在輸入時就不用區分大小寫了。不然用戶體驗會很麻煩,這是我個人理解。
6. PHP如何生成加減演算法方式的驗證碼
<?php
namespace mobile\components;
/**
* @author fenghuo
*
* 改造的加減法驗證類
* 使用示例 VerifyCode::get(1,2);
* 驗證示例 VerifyCode::check($code);
*/
class VerifyCode
{
/**
* php驗證碼
*/
public static function get($one,$two,$prefix = '', $font_size = 28)
{
//文件頭...
ob_get_clean();
header("Content-type: image/png;charset=utf-8;");
//創建真彩色白紙
$width = $font_size*5;
$height = $font_size+1;
$im = @imagecreatetruecolor($width, $height) or die("建立圖像失敗");
//獲取背景顏色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景顏色
imagefill($im, 0, 0, $background_color);
//獲取邊框顏色
$border_color = imagecolorallocate($im, 200, 200, 200);
//畫矩形,邊框顏色200,200,200
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);
//逐行炫耀背景,全屏用1或0
for($i = 2;$i < $height - 2;$i++) {
//獲取隨機淡色
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
//畫線
imageline($im, 2, $i, $width - 1, $i, $line_color);
}
//設置印上去的文字
$firstNum = $one;
$secondNum = $two;
$actionStr = $firstNum > $secondNum ? '-' : '+';
//獲取第1個隨機文字
$imstr[0]["s"] = $firstNum;
$imstr[0]["x"] = rand(2, 5);
$imstr[0]["y"] = rand(1, 4);
//獲取第2個隨機文字
$imstr[1]["s"] = $actionStr;
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
$imstr[1]["y"] = rand(1,5);
//獲取第3個隨機文字
$imstr[2]["s"] = $secondNum;
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
$imstr[2]["y"] = rand(1, 5);
//獲取第3個隨機文字
$imstr[3]["s"] = '=';
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
$imstr[3]["y"] = 3;
//獲取第3個隨機文字
$imstr[4]["s"] = '?';
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
$imstr[4]["y"] = 3;
//文字
$text = '';
//寫入隨機字串
for($i = 0; $i < 5; $i++) {
//獲取隨機較深顏色
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));
$text .= $imstr[$i]["s"];
//畫文字
imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);
}
session_start();
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);
//顯示圖片
ImagePng($im);
//銷毀圖片
ImageDestroy($im);
}
public static function check($code)
{
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {
return true;
} else {
return false;
}
}
7. php問題,關於生成驗證碼的問題, 在下面代碼的第8行,為什麼要 用dechex函數把它轉換成十進制
說反了,是轉換成16進制,如果不轉換16進制那用你這個方法產生的驗證碼都是一些數字的組合,這里估計目的就是為了得到由(0-9,A-F)這幾個字元組成的驗證碼吧。
8. php 或者 js用戶輸入完驗證碼怎麼驗證
先將隨機的驗證碼數字保存到服務端的session空間里,用戶提交後先獲取session空間里的數字再跟表單的數字對比就可以了。
9. php中如何使用隨機函數rand()生成一個數字驗證碼
如果要生成四位數字的驗證碼,則可以用函數:
$srand = rand(1000,9999);
會生成在1000到9999之間的隨機數字,如果要生成更多位數的數字,可以更改最小、最大值。