3、驗(yàn)證碼
封裝的驗(yàn)證碼類?
<?php
/*
* 生成驗(yàn)證碼
*/
class
Captcha
{
private
$_width
= 100;
private
$_height
= 25;
private
$_number
= 4;
//顯示的驗(yàn)證碼的字符個(gè)數(shù)
private
$_font
= 15;
//驗(yàn)證碼字體大小
private
$_fontfile
=
'STXINWEI.TTF'
;
//創(chuàng)建驗(yàn)證碼圖像
public
function
makeImage()
{
# 1. 創(chuàng)建圖像資源(畫布)
$image
= imagecreatetruecolor(
$this
->_width,
$this
->_height);
//隨機(jī)填充顏色
//mt_rand(0,255) 生成一個(gè)更具有唯一性的隨機(jī)數(shù) #000 255
$color
= imagecolorallocate(
$image
,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
imagefill(
$image
,0,0,
$color
);
# 2.繪制文字
$code
=
$this
-> makeCode();
//隨機(jī)生成驗(yàn)證碼文字 ab3g
$color
= imagecolorallocate(
$image
,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
for
(
$i
=0;
$i
<
$this
->_number;
$i
++){
imagettftext(
$image
,
$this
->_font,mt_rand(-30,30),
$i
*(
$this
->_width/
$this
->_number)+5,20,
$color
,
$this
->_fontfile,
$code
[
$i
]);
}
# 3.繪制15條干擾線條
for
(
$i
=0;
$i
<10;
$i
++){
$color
= imagecolorallocate(
$image
,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
imageline(
$image
,mt_rand(0,
$this
->_width),mt_rand(0,
$this
->_height),mt_rand(0,
$this
->_width),mt_rand(0,
$this
->_height),
$color
);
}
# 4.設(shè)置100個(gè)干擾像素點(diǎn)
for
(
$i
=0;
$i
<100;
$i
++){
imagesetpixel(
$image
,mt_rand(0,
$this
->_width),mt_rand(0,
$this
->_height),
$color
);
}
# 5.將驗(yàn)證碼保存起來嗎,便于后面再其他地方使用
//只能使用session來存儲(chǔ),session明天就會(huì)講到
session_start();
$_SESSION
[
'captcha'
] =
$code
;
//在瀏覽器輸出、顯示一下
header(
"Content-Type:image/png"
);
imagepng(
$image
);
imagedestroy(
$image
);
}
/**
* 隨機(jī)產(chǎn)生隨機(jī)數(shù)
*/
public
function
makeCode()
{
# 獲得字母的范圍(大寫字母、小寫字母)
$lower
= range(
'a'
,
'z'
);
//創(chuàng)建從小a到小z字符范圍的數(shù)組
$upper
= range(
'A'
,
'Z'
);
//創(chuàng)建從大A到大Z范圍的數(shù)組
$number
= range(3,9);
//創(chuàng)建從3到9之間的數(shù)字
//將上面的三個(gè)數(shù)組合并成一個(gè)數(shù)組
$code
=
array_merge
(
$lower
,
$upper
,
$number
);
# 打亂數(shù)組元素的順序
shuffle(
$code
);
//隨機(jī)從上面的數(shù)組中篩選出n個(gè)字符,需要通過下標(biāo)來取數(shù)組的元素
$str
=
''
;
for
(
$i
=0;
$i
<
$this
->_number;
$i
++){
$str
.=
$code
[
$i
];
}
return
$str
;
}
/**
* 驗(yàn)證用戶輸入的驗(yàn)證碼和我們生產(chǎn)的驗(yàn)證碼是否一致
* @param [str] $input [輸入驗(yàn)證碼值]
* @return
*/
public
function
checkCode(
$input
)
{
session_start();
if
(
strtolower
(
$code
) ==
strtolower
(
$_SESSION
[
'captcha'
])){
//說明驗(yàn)證碼正確
//echo '驗(yàn)證碼正確';
return
true;
}
else
{
//echo '驗(yàn)證碼錯(cuò)誤';
return
false;
}
}
}
?>
實(shí)例 - 驗(yàn)證碼驗(yàn)證(結(jié)合上面的驗(yàn)證類)
html頁面?
<
form
action
=
"captcha.php?act=verify"
method
=
"post"
>
驗(yàn)證碼:<
input
type
=
"text"
name
=
"captcha"
>
<
img
src
=
"captcha.php?act=show"
>
<
br
>
<
input
type
=
"submit"
value
=
"提交"
>
</
form
>
驗(yàn)證碼檢測(cè) captcha.php 頁面?
//接收地址欄上面的參數(shù)
if
(
$_GET
[
'act'
]==
'verify'
){
//說明是提交的表單
//接收表單中用戶輸入的內(nèi)容
$code
=
$_POST
[
'captcha'
];
//和創(chuàng)建的驗(yàn)證碼進(jìn)行比較
session_start();
//將用戶輸入的驗(yàn)證碼 和 我們創(chuàng)建的統(tǒng)一小寫之后再進(jìn)行比較
if
(
strtolower
(
$code
) ==
strtolower
(
$_SESSION
[
'captcha'
])){
//說明驗(yàn)證碼正確
echo
'驗(yàn)證碼正確'
;
}
else
{
echo
'驗(yàn)證碼錯(cuò)誤'
;
}
}
else
if
(
$_GET
[
'act'
]==
'show'
){
//說明需要顯示一個(gè)圖片
require
'Captcha.class.php'
;
$captcha
=
new
Captcha();
$captcha
-> makeImage();
}
- PHP實(shí)現(xiàn)高清晰度無損圖片壓縮功能的代碼
- 用PHP處理png圖片白色背景色改為透明色的實(shí)例代碼
- 關(guān)于PHP往mysql數(shù)據(jù)庫中批量插入數(shù)據(jù)實(shí)例教程
- Php兩點(diǎn)地理坐標(biāo)距離的計(jì)算方法和具體代碼
- PHP獲取HTTP body內(nèi)容的方法
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)中獲取對(duì)象屬性的3種方法實(shí)例分析
- php5.5新增的yield關(guān)鍵字功能與相關(guān)使用技巧
- Windows7下IIS+php配置教程詳細(xì)介紹
- PHP序列化的四種實(shí)現(xiàn)方法與橫向?qū)Ρ冉坛?/a>
- PHP基于Redis消息隊(duì)列實(shí)現(xiàn)的消息推送的方法
分享到:
投訴收藏