導航:首頁 > 編程語言 > php製作驗證碼

php製作驗證碼

發布時間:2022-07-20 04:25:41

php製作注冊頁面驗證碼

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

⑵ 怎麼用原生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 繪制網站登錄首頁圖片驗證碼

幾乎所有的網站登錄頁都會有驗證碼,驗證碼是一種安全保護機制,在注冊時要求必須有人工操作進行驗證,用於防止垃圾注冊機大量注冊用戶賬號佔用伺服器內存從而使伺服器癱瘓。
圖片驗證碼的實現十分簡單。首先從指定字元集合中隨機抽取固定數目的字元,以一種不規則的方法畫在畫布上,再適當添加一些干擾點和干擾元素,最後將圖片輸出,一張嶄新的驗證碼就完成了。
先給大家展示下生成的驗證碼:

點擊刷新:

如果大家對實現效果非常滿意,請繼續往下看。
前端代碼如下:
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="content-type"
content="text/html;charset=utf-8">
<title>This
is
a
test!</title>
<link
rel="stylesheet"
type="text/css"
href="css/bootstrap.min.css">
</head>
<body>
<form
name="form">
<input
type="text"
placeholder="賬號"/><br/>
<input
type="password"
placeholder="密碼"/><br/>
<input
type="text"
placeholder="驗證碼"/>
<img
id="verImg"
src="libs/verification.php"/>
<a
href="#"
class="change"
onclick="changeVer()">點擊刷新</a><br/>
<input
type="submit"
value="登錄"/>
</form>
<script
type="text/javascript">
//刷新驗證碼
function
changeVer(){
document.getElementById("verImg").src="libs/verification.php?tmp="+Math.random();
}
</script>
</body>
</html>
php腳本文件驗證碼的代碼如下:
<?php
session_start();
//開啟session記錄驗證碼數據
vCode(4,
15);//設置驗證碼的字元個數和圖片基礎寬度
//vCode
字元數目,字體大小,圖片寬度、高度
function
vCode($num
=
4,
$size
=
20,
$width
=
0,
$height
=
0)
{
!$width
&&
$width
=
$num
*
$size
*
4
/
5
+
15;
!$height
&&
$height
=
$size
+
10;
//設置驗證碼字元集合
$str
=
"";
//保存獲取的驗證碼
$code
=
''
//隨機選取字元
for
($i
=
0;
$i
<
$num;
$i++)
{
$code
.=
$str[mt_rand(0,
strlen($str)-1)];
}
//創建驗證碼畫布
$im
=
imagecreatetruecolor($width,
$height);
//背景色
$back_color
=
imagecolorallocate($im,
mt_rand(0,100),mt_rand(0,100),
mt_rand(0,100));
//文本色
$text_color
=
imagecolorallocate($im,
mt_rand(100,
255),
mt_rand(100,
255),
mt_rand(100,
255));
imagefilledrectangle($im,
0,
0,
$width,
$height,
$back_color);
//
畫干擾線
for($i
=
0;$i
<
5;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagearc($im,
mt_rand(-
$width,
$width),
mt_rand(-
$height,
$height),
mt_rand(30,
$width
*
2),
mt_rand(20,
$height
*
2),
mt_rand(0,
360),
mt_rand(0,
360),
$font_color);
}
//
畫干擾點
for($i
=
0;$i
<
50;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagesetpixel($im,
mt_rand(0,
$width),
mt_rand(0,
$height),
$font_color);
}
//隨機旋轉角度數組
$array=array(5,4,3,2,1,0,-1,-2,-3,-4,-5);
//
輸出驗證碼
//
imagefttext(image,
size,
angle,
x,
y,
color,
fontfile,
text)
@imagefttext($im,
$size
,
array_rand($array),
12,
$size
+
6,
$text_color,
'c:WINDOWSFontssimsun.ttc',
$code);
$_SESSION["VerifyCode"]=$code;
//no-cache在每次請求時都會訪問伺服器
//max-age在請求1s後再次請求會再次訪問伺服器,must-revalidate則第一發送請求會訪問伺服器,之後不會再訪問伺服器
//
header("Cache-Control:
max-age=1,
s-maxage=1,
no-cache,
must-revalidate");
header("Cache-Control:
no-cache");
header("Content-type:
image/png;charset=gb2312");
//將圖片轉化為png格式
imagepng($im);
imagedestroy($im);
}
?>
好了,關於小編給大家介紹的php繪制圖片驗證就給大家介紹這么多,希望對大家有所幫助!

⑷ php 簡訊驗證碼資料庫如何設計

php做簡訊驗證碼,需要將手機號,發送的驗證碼和時間這幾個存到資料庫,在添加到資料庫的時候,要判斷裡面有沒有要存的手機號,有的話,就更新驗證碼和時間,沒有就是添加,在使用驗證碼判定的時候,取出驗證碼和時間,判斷驗證碼是否正確,時間是否在自己設置的有效時間段內,整個過程就是這樣。

⑸ 請教網頁製作中PHP驗證碼實現問題。

在文件1用表單提交給文件2,輸入驗證碼肯定是在表單的輸入框里,輸入好後提交表單時對象是文件2就行了。注意php需要伺服器支持啊,沒伺服器直接當文本打開了。

⑹ 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
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怎麼編寫手機簡訊驗證碼功能

以前在遠標做過你的應用應該是這樣吧,用戶輸入手機號碼,點擊發送簡訊,用戶收到驗證碼,輸入對應的驗證碼 判斷是否正確。

需要:
申請一個簡訊介面,就是點擊發送驗證碼的時候,提交到介面給該號碼下發驗證碼。

技術方面的實現:
1、點擊獲取驗證碼
2、程序ajax post提交到簡訊介面
3、簡訊介面服務商 介面判斷用戶和口令,正確後,下發簡訊給該號碼。
4、用戶輸入號碼,程序判斷驗證碼是否一致。

⑼ php製作驗證碼時候,為什麼整屏都是黑色,只有中間一塊是定義的

分享一個驗證碼的經驗:在用php生成驗證碼不能正常顯示的時候,首先你要直接在瀏覽器中打開這個生成驗證碼的php腳本看一下,如果顯示類似"圖片錯誤"的一些信息,說明你的php程序沒用正常運行,接著,你就把"header("content-type:image/png");"這句話去掉,再刷新這個頁面.你就可以根據報錯一步一步的找到問題並解決它.最後都沒問題了,再把"header"那句話加上,然後再看一下."header("content-type:image/png");"這句話的含義是:告訴瀏覽器"我"是一張圖片.有了這句話,瀏覽器就不會正常的顯示php錯誤

⑽ 驗證碼怎麼用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製作驗證碼相關的資料

熱點內容
程序員在線編譯器 瀏覽:247
山東兼職程序員收費標准 瀏覽:424
物業管理系統項目java源碼 瀏覽:8
林皋pdf 瀏覽:584
vue編譯後命名 瀏覽:729
可以發布命令的 瀏覽:954
安卓系統如何領取國旗 瀏覽:328
chromium編譯linux 瀏覽:469
exe在線反編譯工具 瀏覽:365
imbatest命令 瀏覽:410
android自動連接指定wifi 瀏覽:491
用紙做超簡單又解壓的東西 瀏覽:596
國密2演算法是對稱的嗎 瀏覽:465
nc65伺服器地址配置 瀏覽:522
單片機實驗報告電子琴 瀏覽:744
程序員恢復微信文件代碼 瀏覽:517
有漁python 瀏覽:81
pdf字體加深 瀏覽:206
怎麼做一個minecraft伺服器 瀏覽:771
c語言實現ls命令 瀏覽:663