国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

PHP編寫(xiě)登錄驗(yàn)證碼功能 附調(diào)用方法

2024-05-04 23:46:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了PHP編寫(xiě)登錄驗(yàn)證碼功能,文末附調(diào)用方法,并包含隨機(jī)字符函數(shù),和GD庫(kù)畫(huà)圖函數(shù),感興趣的小伙伴們可以參考一下
 

本文實(shí)例為大家分享了一個(gè)PHP寫(xiě)的登錄驗(yàn)證碼功能,供大家參考,具體內(nèi)容如下

 ShowKey.php

<?phpsession_start();//設(shè)置COOKIE或Sessionfunction esetcookie($name,$str,$life=0){//本函數(shù)將字符串 str 全部變小寫(xiě)字符串使驗(yàn)證碼輸入不區(qū)分大小寫(xiě)----在提交表單進(jìn)行session比較同樣需要次函數(shù)轉(zhuǎn)化 $_SESSION[$name]=strtolower($str);}//獲取隨機(jī)字符 此函數(shù)區(qū)分字符大小寫(xiě) 如果不區(qū)分大小寫(xiě)可加入函數(shù)strtolowerfunction domake_password($len) {   $chars = array(     /*"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", "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",*/ "0", "1", "2",     "3", "4", "5", "6", "7", "8", "9"   );   $charsLen = count($chars) - 1;   shuffle($chars);// 將數(shù)組打亂  $output = "";   for ($i=0; $i<$len; $i++)   {     $output .= $chars[mt_rand(0, $charsLen)]; //獲得一個(gè)數(shù)組元素  }   return $output;} //顯示驗(yàn)證碼function ShowKey(){ $key=domake_password(4);//獲取隨機(jī)值 $set=esetcookie("checkkey",$key);//將隨機(jī)值寫(xiě)入cookie或session //是否支持gd庫(kù) if(function_exists("imagejpeg"))  {  header ("Content-type: image/jpeg");  $img=imagecreate(47,20);  $blue=imagecolorallocate($img,102,102,102);  $white=ImageColorAllocate($img,255,255,255);  $black=ImageColorAllocate($img,71,71,71);  imagefill($img,0,0,$blue);  imagestring($img,5,6,3,$key,$white);  for($i=0;$i<90;$i++) //加入干擾象素  {   imagesetpixel($img,rand()%70,rand()%30,$black);  }  imagejpeg($img);  imagedestroy($img); } elseif (function_exists("imagepng")) {  header ("Content-type: image/png");  $img=imagecreate(47,20);  $blue=imagecolorallocate($img,102,102,102);  $white=ImageColorAllocate($img,255,255,255);  $black=ImageColorAllocate($img,71,71,71);  imagefill($img,0,0,$blue);  imagestring($img,5,6,3,$key,$white);  for($i=0;$i<90;$i++) //加入干擾象素  {   imagesetpixel($img,rand()%70,rand()%30,$black);  }  imagepng($img);  imagedestroy($img); } elseif (function_exists("imagegif"))  {  header("Content-type: image/gif");  $img=imagecreate(47,20);  $blue=imagecolorallocate($img,102,102,102);  $white=ImageColorAllocate($img,255,255,255);  $black=ImageColorAllocate($img,71,71,71);  imagefill($img,0,0,$blue);  imagestring($img,5,6,3,$key,$white);  for($i=0;$i<90;$i++) //加入干擾象素  {   imagesetpixel($img,rand()%70,rand()%30,$black);  }  imagegif($img);  imagedestroy($img); } elseif (function_exists("imagewbmp"))  {  header ("Content-type: image/vnd.wap.wbmp");  $img=imagecreate(47,20);  $blue=imagecolorallocate($img,102,102,102);  $white=ImageColorAllocate($img,255,255,255);  $black=ImageColorAllocate($img,71,71,71);  imagefill($img,0,0,$blue);  imagestring($img,5,6,3,$key,$white);  for($i=0;$i<90;$i++) //加入干擾象素  {   imagesetpixel($img,rand()%70,rand()%30,$black);  }  imagewbmp($img);  imagedestroy($img); } else {  //不支持驗(yàn)證碼  header("content-type:image/jpeg/r/n");  header("Pragma:no-cache/r/n");  header("Cache-Control:no-cache/r/n");  header("Expires:0/r/n");  $fp = fopen("data/vdcode.jpg","r");  }}ShowKey();?>

調(diào)用方法:

 

復(fù)制代碼代碼如下:
<img src="ShowKey.php" name="KeyImg" id="KeyImg"  onClick="KeyImg.src='ShowKey.php?'+Math.random()"> 

 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。



注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 桐乡市| 贡觉县| 黄石市| 涪陵区| 中宁县| 兴业县| 锡林郭勒盟| 灵山县| 苍山县| 汾西县| 阳东县| 囊谦县| 密云县| 金华市| 阿城市| 商城县| 沙田区| 天津市| 南涧| 紫金县| 永嘉县| 玉屏| 营口市| 武城县| 桂东县| 台湾省| 吉木乃县| 青冈县| 岚皋县| 诏安县| 永兴县| 洞头县| 牡丹江市| 朝阳县| 申扎县| 体育| 淮滨县| 徐闻县| 峨山| 曲松县| 嘉义市|