導航:首頁 > 編程語言 > php怎麼做驗證碼

php怎麼做驗證碼

發布時間:2022-10-23 03:53:54

『壹』 php的驗證碼提示怎樣製作

一般製作驗證碼會按照下面的幾步走:
一:創建出來一個圖片,通常我們成為源,可以用imagecreatetruecolor()這個函數搞定
二:給這個源 添加背景色,同時設置文本顯示的顏色,GD庫函數為我們提供了imagecolorallocate()函數
三:材料弄好了,我們要給它添點內容了,就是我們隨機生成的數字或者字母,甚至可以是它們的組合,這里我們可以選擇兩個函數 imagettftext()、imagesrting(),這兩個函數的不同,我們會在後面講解。
例:
<?php
session_start();//開啟session,用來記錄獲得的驗證碼,這個函數要寫在程序的開頭,不然會出現錯誤
header(「Content-type :image/gif」);//把文件的返回類型設為image/gif格式,這個格式可以輸出圖片
$codelen=4;//設置你要讓用戶輸入字元的個數,一般為4,過長用戶體驗不好。
$charset =」ABCDEFGHKLMNPRSTUVWYZ23456789″;//我們可以盡量把一些難以辨認的字元去掉,比如阿拉伯數字0和字母o,這也是提高用戶體驗的一種方法。
$code =」;
for($i=0;$i<$codelen;$i++){//用for循環得到4個隨機的字元,在這里用到了mt_rand,這個函數比rand的效率要高的多,建議大家用這個
$code .=$charset{mt_rand(0,strlen($charset)-1)};
}
$_SESSION['code']=$code;//下篇關於session驗證的文章將會用到
$width = 80;
$height = 40;
$im = imagecreatetruecolor($width,$height);//用imagecreatetruecolor()函數來建立一個新的圖片,裡面的兩個數值分別是寬度和高度,這是製作驗證碼的第一步
$bg = imagecolorallocate($im,255,255,0); //圖片背景的顏色,這里是第二步
$textcolor = imagecolorallocate($im,255,0,0);//文字的顏色
imagefill($im,0,0,$bg);//給圖片填充背景色
//好了上面的鋪墊任務做的差不多了,現在關鍵就是讓字元顯示在圖片上,這里有兩種方法我們一一介紹。
$font =」ggbi.ttf」;//如果你有字體的話 就填上字體的相對路徑,如果沒有就留空。下面的兩個用法我會一一講解。
if($font!==」"){
for($num=0;$num<4;$num++){
imagettftext($im,mt_rand(12,16),(mt_rand(0,75)+330)%360,5+15*$num,20+mt_rand(2,5),$textcolor,$font,$code[$num]);//這里是第三步
}
}
else{
for($num=0;$num<4;$num++){
imagestring($im,5,10+15*$num,10+mt_rand(0,5),$code[$num],$textcolor);
}
}
header(「Content-type: image/jpeg」);
imagejpeg($im);
?>

『貳』 怎麼用PHP寫個驗證碼

首先,當用戶打開頁面時隨機產生一個session,然後根據這個值生成驗證碼圖片。
第二,將驗證碼圖片顯示到表單上。
第三,當用戶提交時表單時,比較session里的值與表單中驗證碼的值進行比較。
簡單的實現過程:http://www.nowamagic.net/php/php_CheckCode.php
復雜的驗證碼圖片生成:http://www.admin5.com/article/20080314/75984.shtml

『叄』 怎樣製作PHP驗證碼

<?php
//驗證碼:文本類型為圖像
header("content-type:image/png");
define('TYPE',3);//1.字母 2.字母數字 3.數字 4.邏輯 5.漢字
session_start();
//創建畫布
$img = imagecreatetruecolor(90,33);
//創建顏色
//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$bgcolor = imagecolorallocate($img,255,255,255);
$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
//填充顏色到畫布
imagefill($img,0,0,$bgcolor);
//創建但像素的點為干擾項
//for($i=0;$i<100;$i++){
// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));
// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);
//}
//
////劃線
//$linecolor = imagecolorallocate($img,255,0,0);
//imageline($img,0,0,70,30,$linecolor);
//
////多邊形
// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );
// imagepolygon ( $img ,
// array (
// 5 , 5,
// 5, 15 ,
// 20,15,
// 20,5
// ),
// 4 ,
// $col_poly );
////弧線
//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );
//imagearc($img,35,15,30,30,0,360,$arcColor);

//創建驗證碼的內容
//#字母
$letter = range('A','Z');
$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];
//數字字母
$num = range(0,9);
$numberAndLetter = array_merge($letter,$num);
$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];
//#數字
$number = rand(1000,9999);
//#邏輯
$x = rand(1,9);
$y = rand(1,9);
$expression = $x."+".$y."=?";
$sum = $x+$y;
//#漢字
$CH = array('恭喜發財','財源滾滾','財源廣進','才高八斗','學富五車','抬頭見喜');
$chstr = $CH[rand(0,count($CH)-1)];
switch(TYPE){
case 1 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$letterStr);$_SESSION['code']=$letterStr;break;
case 2 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$nal);$_SESSION['code']=$nal;break;
case 3 : imagettftext($img,14,0,13,23,$textcolor,'MSYH.TTF',$number);$_SESSION['code']=$number;break;
case 4 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$expression);$_SESSION['code']=$sum;break;
case 5 : imagettftext($img,11,0,6,21,$textcolor,'MSYHBD.TTF',$chstr);$_SESSION['code']=$chstr;break;
}
//輸入圖像到瀏覽器
imagepng($img);
?>

『肆』 怎麼樣能夠讓php生成4位數字驗證碼

PHP生成驗證碼的原理:使用PHP的GD庫,生成一張帶驗證碼的圖片,並將驗證碼保存在Session中。PHP生成驗證碼的大致流程有: 1、產生一張png的圖片; 2、為圖片設置背景色; 3、設置字體顏色和樣式; 4、產生4位數的隨機的驗證碼; 5、把產生的每...

『伍』 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製作注冊頁面驗證碼

使用view-source:查看源碼會有錯誤提示,所以導致圖片無法正常輸出

『柒』 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製作驗證碼

<?php

//驗證碼類
classValidateCode{
private$charset='';//隨機因子
private$code;//驗證碼
private$codelen=4;//驗證碼長度
private$width=90;//寬度
private$height=40;//高度
private$img;//圖形資源句柄
private$font;//指定的字體
private$fontsize=20;//指定字體大小
private$fontcolor;//指定字體顏色
//構造方法初始化
publicfunction__construct(){
$this->font=dirname(__FILE__).'/font/elephant.ttf';//注意字體路徑要寫對,否則顯示不了圖片
}
//生成隨機碼
privatefunctioncreateCode(){
$_len=strlen($this->charset)-1;
for($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$_len)];
}
}
//生成背景
privatefunctioncreateBg(){
$this->img=imagecreatetruecolor($this->width,$this->height);
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
privatefunctioncreateFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成線條、雪花
privatefunctioncreateLine(){
//線條
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//雪花
for($i=0;$i<100;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//輸出
privatefunctionoutPut(){
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//對外生成
publicfunctiondoimg(){
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//獲取驗證碼
publicfunctiongetCode(){
returnstrtolower($this->code);
}
}

『玖』 怎麼用原生php做驗證碼

<?php
class validateCode{
private $charset=''; //隨機因子
private $_len;
private $code; //驗證碼
private $codelen=4; //驗證碼長度
private $width=130; //畫布寬度
private $height=50; //畫布長度
private $img; //圖形資源句柄
private $font; //制定字體
private $fontsize=26;//字體大小
private $fontcolor; //字體顏色

//初始化構造函數
public function __construct(){
$this->font=dirname(__file__).'/validateFont.ttf';
//字體文件及路徑,__file__表示當前PHP文件絕對路徑,dirname表示這個PHP文件的絕對路徑的上一層
}

//生成隨機碼
private function createCode(){
$this->_len=strlen($this->charset)-1; //strlen表示計算字元串長度
for($i=1;$i<=$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$this->_len)];
}
}
//生成背景
private function createBG(){
$this->img=imagecreatetruecolor($this->width,$this->height);//創建圖像
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255)); //分配顏色函數(紅,綠,藍)
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); //矩形區域著色函數(要填充的圖像,x1,y1,x2,y2,要填充的顏色),x1,y1,x2,y2表示矩形的對角線
}

//生成文字
private function createFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]); //參數 size 為字形的尺寸;angle 為字型的角度,順時針計算,0 度為水平,也就是三點鍾的方向 (由左到右),90 度則為由下到上的文字;x,y 二參數為文字的坐標值 (原點為左上角);參數 col 為字的顏色;fontfile 為字型文件名稱,亦可是遠端的文件;text 當然就是字元串內容了。
}
}

//生成線條雪花
private function createLine(){
//線條
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}

//雪花
for($i=0;$i<40;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}

//輸出
private function outPut(){
header("content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}

//對外生成
public function doimg(){
$this->createCode();
$this->createBg();
$this->createFont();
$this->createLine();
$this->outPut();
}

//獲取驗證碼
public function getCode(){
return strtolower($this->code);
}
}
?>
字體自己去找,修改下路徑就行了

『拾』 如何實現php手機簡訊驗證功能

現在網站在建設網站時為了保證用戶信息的真實性,往往會選擇發簡訊給用戶手機發驗證碼信息,只有通過驗證的用戶才可以注冊,這樣保證了用戶的聯系信息資料的100%的准確性。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<html xmlns>

<head>

<title></title>

<script src="js/jquery-1.4a2.min.js" type="text/javascript"></script>

<script type="text/javascript">

/*-------------------------------------------*/

var InterValObj; //timer變數,控制時間

var count = 60; //間隔函數,1秒執行

var curCount;//當前剩餘秒數

var code = ""; //驗證碼

var codeLength = 6;//驗證碼長度

function sendMessage() {

curCount = count;

var dealType; //驗證方式

tel = $(』#tel』).val();

if(tel!=』』){

//驗證手機有效性

var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+d{8})$/;

if(!myreg.test($(』#tel』).val()))

{

alert(』請輸入有效的手機號碼!』);

return false;

}

tel = $(』#tel』).val();

//產生驗證碼

for (var i = 0; i < codeLength; i++) {

code += parseInt(Math.random() * 9).toString();

}

//設置button效果,開始計時

$("#btnSendCode").attr("disabled", "true");

$("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");

InterValObj = window.setInterval(SetRemainTime, 1000); //啟動計時器,1秒執行一次

//向後台發送處理數據

$.ajax({

type: "POST", //用POST方式傳輸

dataType: "text", //數據格式:JSON

url: 』yanzhengma.php』, //目標地址(根據實際地址)

data: "&tel=" + tel + "&code=" + code,

error: function (XMLHttpRequest, textStatus, errorThrown) { },

success: function (msg){ }

});

}else{

alert(』請填寫手機號碼』);

}

}

//timer處理函數

function SetRemainTime() {

if (curCount == 0) {

window.clearInterval(InterValObj);//停止計時器

$("#btnSendCode").removeAttr("disabled");//啟用按鈕

$("#btnSendCode").val("重新發送驗證碼");

code = ""; //清除驗證碼。如果不清除,過時間後,輸入收到的驗證碼依然有效

}

else {

curCount--;

$("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");

}

}

</script>

</head>

<body>

<input name="tel" id=tel type="text" />

<input id="btnSendCode" type="button" value="發送驗證碼" onclick="sendMessage()" /></p>

</body>

</html>


第三、調用簡訊伺服器簡訊介面

整理的頁面是yanzhengma.php(具體根據服務商提供信息)

<?php //提交簡訊

$post_data = array();

$post_data[』userid』] =簡訊服務商提供ID;

$post_data[』account』] = 』簡訊服務商提供用戶名』;

$post_data[』password』] = 』簡訊服務商提供密碼』;

// Session保存路徑

$sessSavePath = dirname(__FILE__)."/../data/sessions/";

if(is_writeable($sessSavePath) && is_readable($sessSavePath)){

session_save_path($sessSavePath);

}

session_register(』mobliecode』);

$_SESSION[』mobilecode』] = $_POST["code"];

$content=』簡訊驗證碼:』.$_POST["code"].』【簡訊驗證】』;

$post_data[』content』] = mb_convert_encoding($content,』utf-8』, 』gb2312』); //簡訊內容需要用urlencode編碼下

$post_data[』mobile』] = $_POST["tel"];

$post_data[』sendtime』] = 』』; //不定時發送,值為0,定時發送,輸入格式YYYYMMDDHHmmss的日期值

$url=』http://IP:8888/sms.aspx?action=send』;

$o=』』;

foreach ($post_data as $k=>$v)

{

$o.="$k=".$v.』&』;

}

$post_data=substr($o,0,-1);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要將結果直接返回到變數里,那加上這句。

$result = curl_exec($ch);

?>


第四:提交表單信息時對簡訊驗證碼驗證

//手機驗證碼開始

session_start();

$svalitel = $_SESSION[』mobilecode』];

$vdcodetel = empty($vdcodetel) ? 』』 : strtolower(trim($vdcodetel));

if(strtolower($vdcodetel)!=$svalitel || $svalitel==』』)

{

ResetVdValue();

//echo "Pageviews=".$vdcodetel;

ShowMsg("手機驗證碼錯誤!", 』-1』);

exit();

}

閱讀全文

與php怎麼做驗證碼相關的資料

熱點內容
部隊抗洪搶險命令範文 瀏覽:884
歐姆龍plc編程軟體使用教程 瀏覽:590
ai文件pdf 瀏覽:911
騰訊雲伺服器掛載混合雲 瀏覽:758
智能小車用什麼單片機 瀏覽:463
java怎麼給窗口關閉 瀏覽:940
列舉51單片機的定址方式 瀏覽:706
剪輯app怎麼寫長篇文字 瀏覽:400
app專屬流量過月租怎麼不更新 瀏覽:654
王者程序員都有誰 瀏覽:76
給牛換腳掌解壓 瀏覽:387
圍棋有多少種演算法 瀏覽:602
unity資源包在哪個文件夾 瀏覽:704
阿里雲伺服器遠程鏈接不成功 瀏覽:482
文件系統pdf 瀏覽:766
原神安卓區服什麼意思 瀏覽:37
貝殼app怎麼線上發布 瀏覽:159
如何挑選安卓系統機頂盒 瀏覽:54
安卓快充使用有什麼注意事項 瀏覽:909
黑馬程序員的雲計算網課 瀏覽:948