❶ thinkphp的驗證碼類庫,如果需要使用中文驗證碼,那麼需要將系統字體文件復制到
我的項目代碼:
<?php
publicfunctionverify_code(){
$verify=newThinkVerify();
$verify->__set('seKey',C('VERIFY_KEY'));
$verify->__set('useZh',true);
$verify->__set('bg',array(235,246,230));
$verify->__set('length',2);
$verify->__set('fontSize',14);
$verify->__set('imageH',40);
$verify->__set('imageW',70);
$verify->__set('code',get_word());
return$verify->entry(1);
}
?>
get_word方法是隨機獲取一個兩個字的詞。
另外,字體是TP自帶的。
目錄在:/ThinkPHP/Library/Think/Verify/
❷ 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中文驗證碼的思路是怎樣的是在字元串里輸入很多個中文讓隨機還是怎麼樣
$keyword = $_GET ['q']; //獲取的q值是 "請選擇" $choose="請選擇"; if($keyword==$choose) echo "ok"; echo var_mp($keyword).var_mp($choose); //echo 的結果 $keyword=>string(9) "請選擇" $choose=
❹ 如何用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生成的驗證碼隨機數不重復
如果要生成四位數字的驗證碼,則可以用函數: $srand = rand(1000,9999); 會生成在1000到9999之間的隨機數字,如果要生成更多位數的數字,可以更改最孝最大值。
❻ PHP圖片驗證碼中文內容怎麼寫
function image($string){
session_unregister("string");
session_register("string");
Header("Content-type:image/png");
$imageWidth=80;
$imageHeight=30;
$im=imagecreate($imageWidth,$imageHeight);
$backColor=ImageColorAllocate($im,rand(220,255),rand(220,255));
imagefilledrectangle($im,0,0,$imageWidth,$imageHeight,$backColor);
for($i=0;$i<100;$i++){
$dotColor = ImageColorAllocate($im,rand(0,255),rand(0,255),rand(0,255));
$x = rand(0,$imageWidth);$y = rand(0,$imageHeight);
imagesetpixel($im,$x,$y,$dotColor);
}
for($i=0;$i<strlen($string);$i++){
$frontColor = ImageColorAllocate($im,rand(0,120),rand(0,120),rand(0,120));
imagestring($im,10,rand(20*$i+1,20*$i+10),rand(0,5),substr($string,$i,1),$frontColor);
}
imagepng($im);
imagedestroy($im);
exit();
}
將參數傳入中文字元串
❼ php中文驗證碼無法顯示
session_start();
$image=imagecreatetruecolor(200,60);//創建畫布
$color=imagecolorallocate($image,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));//隨機顏色
//$color=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$color);//填充顏色
//中文驗證碼
$fontface="simhei.ttf";//確保相同目錄下有該字體
$strdb=array('好','多','人','在','學','習');
for($i=0;$i<4;$i++){
$fontsizecolor=imagecolorallocate($image,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
$codex=iconv("GB2312","UTF-8",$strdb[mt_rand(0,5)]);//iconv不能轉數組取任意下標
imagettftext($image,mt_rand(20,24),mt_rand(-30,30),(40*$i+20),mt_rand(30,35),$fontsizecolor,$fontface,$codex);//如果用$code的話就生成1+2+3+4是個漢字的驗證碼了
}
//干擾點
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imagesetpixel($image,mt_rand(1,100),mt_rand(1,20),$pointcolor); //雪花
}
//干擾線
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imageline($image,mt_rand(1,99),mt_rand(1,99),mt_rand(1,99),mt_rand(1,99),$linecolor);
}
ob_clean();
header("Content-type:image/png");
imagepng($image);
imagedestroy($image);
❽ PHP怎麼實現驗證碼中的字母是從a-z大小寫隨機包含。
代碼如下:
<?php
function createRandomStr($length){
$str = array_merge(range('a','z'),range('A','Z'));
shuffle($str);
$str = implode('',array_slice($str,0,$length));
return $str;
}
echo createRandomStr(4);
?>
❾ php中如何使用隨機函數rand()生成一個數字驗證碼
如果要生成四位數字的驗證碼,則可以用函數:
$srand = rand(1000,9999);
會生成在1000到9999之間的隨機數字,如果要生成更多位數的數字,可以更改最小、最大值。