就如今天遇到隨即函數(shù)rand();腦海中想到用它做點(diǎn)啥好呢,最后想起了驗(yàn)證碼,數(shù)字驗(yàn)證碼,字母驗(yàn)證碼,中文驗(yàn)證碼,可是自己不會(huì)呀,咋辦呢,上網(wǎng)搜,看別人的代碼,開不懂,看視頻,聽老師講,將其中所遇到的函數(shù),值得注意的地方都拿筆記下,平常看到一般網(wǎng)頁上的隨機(jī)驗(yàn)證碼都是以一定的方框包圍起來,貌似就是以圖片為背景的。經(jīng)過邊看,自己邊敲,雖然遇到很多不會(huì)的問題,但是我相信只要自己腳踏實(shí)地,一定學(xué)會(huì)的。現(xiàn)在想做一下總結(jié),自己可能寫的很亂,可我相信有一天會(huì)實(shí)現(xiàn)的。1.產(chǎn)生數(shù)字的隨機(jī)數(shù) ——》創(chuàng)建圖片——》隨機(jī)數(shù)寫進(jìn)圖片——》在圖片加入干擾值(點(diǎn),線)——》保持在session中——》在form表單中引用;隨機(jī)函數(shù):rand(int min,int max);萬變不離其宗,我看了網(wǎng)上許多中生成隨機(jī)數(shù)的代碼,有數(shù)字和字母隨機(jī)數(shù),中文隨機(jī)數(shù)(數(shù)組)等等;都離不開rand();代碼如下(有的上網(wǎng)copy,希望各位不要見怪啊第一種:
復(fù)制代碼代碼如下:
$authnum='';
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);//分割函數(shù)
for($i=0;$i<4;$i++){
$randnum=rand(0,35);
$authnum.=$list[$randnum];//以數(shù)組的形式輸出
第二種:
復(fù)制代碼代碼如下:
private function createCheckCode()
{
for(i=0;i<this->codeNum;i++)
{
number = rand(0,2);
switch(number)
{
case 0: rand_number = rand(48,57); break;//數(shù)字
case 1: rand_number = rand(65,90);break;//大寫字母
case 2: rand_number = rand(97,122);break;//小寫字母
}
$asc = sprintf("%c",rand_number);
$asc_number = asc_number.asc;
}
return asc_number;
}
第三種:
復(fù)制代碼代碼如下:
srand(microtime()*100000);//相當(dāng)于計(jì)時(shí)器
$string="abcdefghigklmnopqrstuvwxyz123456789";
for($i=0;$i<4;$i++)
{
$new_number.=$string[rand(0,strlen($string)-1)];//隨即的產(chǎn)生一個(gè)數(shù)組
}
第四種:
復(fù)制代碼代碼如下:
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));//將十進(jìn)制轉(zhuǎn)化為十六進(jìn)制
}
GD庫:(提供了一系列圖片處理函數(shù)的IPI,生成圖片處理圖片)
啟用php中GD庫:php.ini配置文件中,去掉";extension=php_gd2.dll"中“;”;
部分GD庫函數(shù)的介紹:1.imagecreatetruecolor(int x_size,int Y_size) 新建真彩色圖像
2.imagecolorallocate(resource image,int red,int green,int blue) 為一幅圖像分配顏色,三原色
3.imagestring(resource,font,int x,int y,content,color)繪圖函數(shù)4.header("Content-type:image/jpeg") 輸出函數(shù)php的header是定義頭的動(dòng)作,php5中支持3中類型: 1,Content-type:xxxx/yyyy 2,Location:xxxx:yyyy/zzzz 3,Status:nnn xxxxxx xxxx/yyyy表示內(nèi)容文件的類型 如:image/gif image/jpeg image/png imagejpeg(),imagegif(),imagepang() 5.iamgeline(resource image,int x1,int y1,int x2,int y2,int color); 畫線函數(shù),(int x,int y)起始坐標(biāo)6.imagesetpixel(resource image,int x,int y,int color) 畫點(diǎn)函數(shù)7.imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 帶字體寫入函數(shù)8.iconv("gb2312","utf-8","字符串"); //首先要將文字轉(zhuǎn)換成utf-8格式 php驗(yàn)證碼插入中文的方法。
隨機(jī)生成數(shù)字,字母的代碼:
復(fù)制代碼代碼如下:
<?php
//che.php
session_start();
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));
}
$_SESSION['check_num']=$rand;
$image=imagecreatetruecolor(50,30);
$bg=imagecolorallocate($im,0,0,0);//第一次用調(diào)色板的時(shí)候,背景顏色
$te=imagecolorallocate($im,255,255,255);
imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
ob_clean();//PHP網(wǎng)頁中因?yàn)?要生成驗(yàn)證碼而出現(xiàn) 圖像"http://localhost/**.php"因其本身有錯(cuò)無法顯示
header("Content-type:image/jpeg"); imagejpeg($image);
?>
給圖片畫出干擾線代碼:
復(fù)制代碼代碼如下:
for($i=0;$i<8;$i++)//畫出多條線
{
$cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//產(chǎn)生隨機(jī)的顏色
imageline($im,rand(10,40),0,rand(10,40),20,$cg);
}
給圖片畫出干擾點(diǎn)的代碼:
復(fù)制代碼代碼如下:
for($i=0;$i<80;$i++)//畫出多個(gè)點(diǎn)
{
imagesetpixel($im,rand(0,40),rand(0,20),$cg);
}
把文字寫入圖片代碼:
復(fù)制代碼代碼如下:
$str=array('我','我','親','親');//存儲(chǔ)顯示的漢字
for($i=0;$i<4;$i++)
{
$sss.=$str[rand(0,3)];//隨機(jī)顯示漢字
}
//$str=iconv("gb2312","utf-8",$str); //漢字編碼轉(zhuǎn)化,我的好像不需要
imagettftext($im,10,0,rand(5,60),rand(5,60),$te,"simhei.ttf",$sss);//
0:字體的傾斜度,“simhei.ttf”:字體樣式,一般放在根目錄下;