php制作動(dòng)態(tài)隨機(jī)驗(yàn)證碼(2)_PHP教程
推薦:PHP獲取一年中每個(gè)星期的開始和結(jié)束日期的方法這篇文章主要介紹了PHP獲取一年中每個(gè)星期的開始和結(jié)束日期的方法,涉及php對(duì)日期操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了PHP獲取一年中每個(gè)星期的開始和結(jié)束日期的方法。分享給大家供大家參考。具體分析如下: 最近項(xiàng)目中需要做個(gè)提交周
代碼如下: //創(chuàng)建一張圖像
$_img = imagecreatetruecolor($_width,$_height);
//白色
$_white = imagecolorallocate($_img,255,255,255);
//填充
imagefill($_img,0,0,$_white);
if ($_flag) {
//黑色,邊框
$_black = imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
}
4)模糊背景
代碼如下:
//隨機(jī)畫出6個(gè)線條
for ($i=0;$i<6;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}
//隨機(jī)雪花
for ($i=0;$i<100;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
}
5)輸出及銷毀
代碼如下:
//輸出驗(yàn)證碼
for ($i=0;$i<strlen($_SESSION['code']);$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color);
}
//輸出圖像
header('Content-Type: image/png');
imagepng($_img);
//銷毀
imagedestroy($_img);
將其封裝在global.func.php全局函數(shù)庫中,函數(shù)名為_code(),以便調(diào)用。我們將設(shè)置$_width ,$_height ,$_rnd_code,$_flag 四個(gè)參數(shù),以增強(qiáng)函數(shù)的靈活性。
* @param int $_width 驗(yàn)證碼的長(zhǎng)度:如果要6位長(zhǎng)度推薦75+50;如果要8位,推薦75+50+50,依次類推
* @param int $_height 驗(yàn)證碼的高度
* @param int $_rnd_code 驗(yàn)證碼的位數(shù)
* @param bool $_flag 驗(yàn)證碼是否需要邊框:true有邊框, false無邊框(默認(rèn))
封裝后的代碼如下:
代碼如下:
<?php
/**
* [verification-code] (C)2015-2100 jingwhale.
*
* This is a freeware
* $Id: global.func.php 2015-02-05 20:53:56 jingwhale$
*/
/**
* _code()是驗(yàn)證碼函數(shù)
* @access public
* @param int $_width 驗(yàn)證碼的長(zhǎng)度:如果要6位長(zhǎng)度推薦75+50;如果要8位,推薦75+50+50,依次類推
* @param int $_height 驗(yàn)證碼的高度
* @param int $_rnd_code 驗(yàn)證碼的位數(shù)
* @param bool $_flag 驗(yàn)證碼是否需要邊框:true有邊框, false無邊框(默認(rèn))
* @return void 這個(gè)函數(shù)執(zhí)行后產(chǎn)生一個(gè)驗(yàn)證碼
*/
function _code($_width = 75,$_height = 25,$_rnd_code = 4,$_flag = false) {
//創(chuàng)建隨機(jī)碼
for ($i=0;$i<$_rnd_code;$i++) {
$_nmsg .= dechex(mt_rand(0,15));
}
//保存在session
$_SESSION['code'] = $_nmsg;
//創(chuàng)建一張圖像
$_img = imagecreatetruecolor($_width,$_height);
//白色
$_white = imagecolorallocate($_img,255,255,255);
//填充
imagefill($_img,0,0,$_white);
if ($_flag) {
//黑色,邊框
$_black = imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
}
//隨即畫出6個(gè)線條
for ($i=0;$i<6;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}
//隨即雪花
for ($i=0;$i<100;$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
}
//輸出驗(yàn)證碼
for ($i=0;$i<strlen($_SESSION['code']);$i++) {
$_rnd_color = imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color);
}
//輸出圖像
header('Content-Type: image/png');
imagepng($_img);
//銷毀
imagedestroy($_img);
}
?>
2.創(chuàng)建驗(yàn)證機(jī)制
創(chuàng)建php驗(yàn)證頁面,通過session來檢驗(yàn)驗(yàn)證碼是否一致。
1)創(chuàng)建verification-code.php驗(yàn)證頁面
代碼如下:
<?php
/**
* [verification-code] (C)2015-2100 jingwhale.
*
* This is a freeware
* $Id: verification-code.php 2015-02-05 20:53:56 jingwhale$
*/
//設(shè)置字符集編碼
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>verification code</title>
<link rel="stylesheet" type="text/css" href="style/basic.css" />
</head>
<body>
<div id="testcode">
<form method="post" name="verification" action="verification-code.php?action=verification">
<dl>
<dd>驗(yàn)證碼:<input type="text" name="code" class="code" /><img src="codeimg.php" id="codeimg" /></dd>
<dd><input type="submit" class="submit" value="驗(yàn)證" /></dd>
</dl>
</form>
</div>
</body>
</html>
顯示如下:

2)創(chuàng)建產(chǎn)生驗(yàn)證碼圖片頁面
創(chuàng)建codeimg.php為verification-code.php html代碼里的img提供驗(yàn)證碼圖片
首先必須在codeimg.php頁面開啟session;
其次,將我們封裝好的global.func.php全局函數(shù)庫引入進(jìn)來;
最后,運(yùn)行_code();
分享:php模擬post提交數(shù)據(jù)的方法這篇文章主要介紹了php模擬post提交數(shù)據(jù)的方法,實(shí)例分析了socket方法模擬post提交數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了php模擬post提交數(shù)據(jù)的方法。分享給大家供大家參考。具體如下: php模擬post提交數(shù)據(jù),用處很多,可用來網(wǎng)站的采集,
- PHP獲取一年中每個(gè)星期的開始和結(jié)束日期的方法
- php模擬post提交數(shù)據(jù)的方法
- PHP遍歷數(shù)組的三種方法及效率對(duì)比分析
- PHP進(jìn)程同步代碼實(shí)例
- PHP CURL 內(nèi)存泄露問題解決方法
- PHP中捕獲超時(shí)事件的方法實(shí)例
- php單例模式示例分享
- PHP 正則表達(dá)式小結(jié)
- php使用iconv中文截?cái)鄦栴}的解決方法
- php+Mysqli利用事務(wù)處理轉(zhuǎn)賬問題實(shí)例
- php中使用url傳遞數(shù)組的方法
- php使用類繼承解決代碼重復(fù)的問題
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP中使用協(xié)同程序?qū)崿F(xiàn)合作多任務(wù)
- 關(guān)于方便實(shí)用的PHP生成靜態(tài)頁面類的介紹
- 深入php函數(shù)file_get_contents超時(shí)處理的方法詳解
- Zend Framework 入門——多國語言支持
- php中mb_convert_encoding 和 iconv 的區(qū)別
- php實(shí)現(xiàn)gb2312和unicode間編碼轉(zhuǎn)換
- 解析PHP網(wǎng)站開發(fā)中常見的問題
- PHP與正則表達(dá)系列之一: PHP中的正則表達(dá)式
- 基于PHP MySQL的聊天室設(shè)計(jì)
- 利用PHP實(shí)現(xiàn)短域名互轉(zhuǎn)
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php制作動(dòng)態(tài)隨機(jī)驗(yàn)證碼(2)
。