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

php生成驗證碼類

發布時間:2022-07-11 09:56:22

⑴ 在線等 php 的驗證碼 我圖片是生成了 但是 session裡面是空的 是什麼原因啊急等高手解答

試試輸出$number_len這個值看看

⑵ 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;
}
}

⑶ 驗證碼怎麼用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);
?>

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

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

⑸ 請問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圖片中文驗證碼生成問題

重新寫了一下,這里不能print那個字元串..不然會出錯
因為啟動了session 之前不能有輸出
最終$_SESSION['yzm']就是圖片上的驗證碼..
--------------------
<?php
session_start();
$im = imagecreatetruecolor(80,30);
$bg = imagecolorallocate($im,0,0,0);
$te = imagecolorallocate($im,255,255,255);
for($i=0;$i<5;$i++)
{
$te2 = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,80),0,80,rand(0,30),$te2);
}
for($i=0;$i<200;$i++)
{
imagesetpixel($im,rand()%100,rand()%30,$te2);
}
srand((float) microtime() * 10000000);
$input = array("步","步","高","請","轉","到","圖","像","我","非","常","高","興");
$rand_keys = array_rand($input, 4);
$_SESSION['yzm'] = $t = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]].$input[$rand_keys[3]];
imagettftext($im,12,0,10,20,$te,'stxingka.ttf',$t);
header("content-type: image/jpeg");
imagejpeg($im);
?>

⑺ 求 PHP 圖片驗證碼類 給出詳細調用方法 謝謝!!!

[code.php]

<?php

/**

*驗證碼圖片

*/

session_start();

Header("Content-type:image/gif");

/*

*初始化

*/

$border=0;//是否要邊框1要:0不要

$how=4;//驗證碼位數

$w=$how*15;//圖片寬度

$h=20;//圖片高度

$fontsize=10;//字體大小

$alpha="abcdefghijkmnpqrstuvwxyz";//驗證碼內容1:字母

$number="23456789";//驗證碼內容2:數字

$randcode="";//驗證碼字元串初始化

srand((double)microtime()*1000000);//初始化隨機數種子

$im=ImageCreate($w,$h);//創建驗證圖片

/*

*繪制基本框架

*/

$bgcolor=ImageColorAllocate($im,255,255,255);//設置背景顏色

ImageFill($im,0,0,$bgcolor);//填充背景色

if($border)

{

$black=ImageColorAllocate($im,0,0,0);//設置邊框顏色

ImageRectangle($im,0,0,$w-1,$h-1,$black);//繪制邊框

}

/*

*逐位產生隨機字元

*/

for($i=0;$i<$how;$i++)

{

$alpha_or_number=mt_rand(0,1);//字母還是數字

$str=$alpha_or_number?$alpha:$number;

$which=mt_rand(0,strlen($str)-1);//取哪個字元

$code=substr($str,$which,1);//取字元

$j=!$i?4:$j+15;//繪字元位置

$color3=ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));//字元隨即顏色

ImageChar($im,$fontsize,$j,3,$code,$color3);//繪字元

$randcode.=$code;//逐位加入驗證碼字元串

}

/*

*如果需要添加干擾就將注釋去掉

*

*以下for()循環為繪背景干擾線代碼

*/

/*+-------------------------------繪背景干擾線開始--------------------------------------------+*/

for($i=0;$i<5;$i++)//繪背景干擾線

{

$color1=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干擾線顏色

ImageArc($im,mt_rand(-5,$w),mt_rand(-5,$h),mt_rand(20,300),mt_rand(20,200),55,44,$color1);//干擾線

}

/*+-------------------------------繪背景干擾線結束--------------------------------------+*/

/*

*如果需要添加干擾就將注釋去掉

*

*以下for()循環為繪背景干擾點代碼

*/

/*+--------------------------------繪背景干擾點開始------------------------------------------+*/

/*

for($i=0;$i<$how*40;$i++)//繪背景干擾點

{

$color2=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干擾點顏色

ImageSetPixel($im,mt_rand(0,$w),mt_rand(0,$h),$color2);//干擾點

}

*/

/*+--------------------------------繪背景干擾點結束------------------------------------------+*/

//把驗證碼字元串寫入session方便提交登錄信息時檢驗驗證碼是否正確例如:$_POST['randcode']=$_SESSION['randcode']

$_SESSION['randcode']=$randcode;

/*繪圖結束*/

Imagegif($im);

ImageDestroy($im);

/*繪圖結束*/

?>

[調用方法]

<SCRIPTLANGUAGE="JavaScript">

<!--

functionreloadcode(){

vard=newDate();

document.getElementById('safecode').src="/code.php?t="+d.toTimeString()

}

//-->

</SCRIPT>

驗證碼:<inputname="chknumber"type="text"maxlength="4"class="chknumber_input"/><imgsrc='code.php'id="safecode"onclick="reloadcode()"title="看不清楚?點擊切換!"></img>

⑻ 用php生成驗證碼,無法顯示

header("content-type:image/png");生成圖像後面加這句話在輸出到瀏覽器試試

⑼ php生成的驗證碼10分鍾內有效如何做

把驗證碼生成的時候寫入資料庫,包含當前時間。提交的時候做判斷用提交的時間減去生成的時間。超過提示無效。

⑽ PHP製作驗證碼時候只顯示一個白色框框

<?php
ob_clean();
//header("Content-type:text/html");
header("Content-type:image/jpeg");//以jpeg格式輸出,注意上面不能輸出任何字元,否則出錯
$w=100;//設置圖片寬和高
$h=50;
$fontsize=16;//設置字體大小
$str=Array();//用來存儲隨機碼
$string="";//隨機挑選其中4個字元,也可以選擇更多,注意循環的時候加上,寬度適當調整
$vcode='';
for($i=0;$i<4;$i++){
$str[$i]=$string[rand(0,strlen($string)-1)];
$vcode.=$str[$i];
}
//echo$vcode;
session_start();//啟用超全局變數session
//session_register("vcode");
$_SESSION["vcode"]=$vcode;
$im=imagecreatetruecolor($w,$h);//創建圖片畫布
$white=imagecolorallocate($im,255,255,255);//第一次調用設置背景色
$black=imagecolorallocate($im,0,0,0);//邊框顏色
imagefilledrectangle($im,0,0,$w,$h,$white);//畫一矩形填充
imagerectangle($im,0,0,$w-1,$h-1,$black);//畫一矩形框
$fontsrc="consola.ttf";
//生成雪花背景
for($i=1;$i<200;$i++){
$x=mt_rand(1,$w-9);
$y=mt_rand(1,$h-9);
$color=imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagechar($im,1,$x,$y,"*",$color);

}
//將驗證碼寫入圖案
for($i=0;$i<count($str);$i++){
$x=13+$i*($w-15)/4;
$y=mt_rand(25,$h-15);
$color=imagecolorallocate($im,mt_rand(0,225),mt_rand(0,150),mt_rand(0,225));
//imagechar($im,$fontsize,$x,$y,$str[$i],$color);
imagettftext($im,$fontsize,0,$x,$y,$color,$fontsrc,$str[$i]);
}
imagejpeg($im);//輸出圖片文件
imagedestroy($im);//銷毀釋放資源
?>

閱讀全文

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

熱點內容
awss3命令 瀏覽:356
百度店鋪客戶訂單手機加密 瀏覽:500
釘釘班群文件夾怎麼上傳文件 瀏覽:749
人社app怎麼解綁手機 瀏覽:101
caj文件夾打不開 瀏覽:475
什麼app可以將電量變色 瀏覽:692
解放出你的解壓抖音小游戲 瀏覽:345
什麼方式解壓比較好 瀏覽:266
erp是什麼伺服器 瀏覽:185
python中tmp 瀏覽:23
說明wpf加密過程 瀏覽:144
java讀取list 瀏覽:703
iis7gzip壓縮 瀏覽:40
有什麼安卓機打吃雞好 瀏覽:598
三星u盤加密狗 瀏覽:474
php函數的返回值嗎 瀏覽:587
國企穩定程序員 瀏覽:328
編程貓如何使用教程視頻 瀏覽:220
安卓遠端網頁如何打日誌 瀏覽:218
壓縮flash大小 瀏覽:993