成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? php生成圖片驗證碼-附五種驗證碼
全站頭部文字 我要出現在這里
php生成圖片驗證碼-附五種驗證碼 752 2023-12-10   

以前輸出驗證碼的時候用過一個方法,在前臺用JS生成驗證碼字符串,再傳遞到后臺用PHP輸出驗證碼圖像。這樣在驗證時就不需要使用$_SESSION傳遞驗證碼的值,直接用JS比較生成的字符串和輸入的字符串是否相等即可。

本文以實例演示5種驗證碼,并介紹生成驗證碼的函數。PHP生成驗證碼的原理:通過GD庫,生成一張帶驗證碼的圖片,并將驗證碼保存在Session中。

查看圖片

1、HTML

5中驗證碼HTML代碼如下:

<div class="demo">


 <h3>1、數字驗證碼</h3>
 <p>驗證碼:<input type="text" class="input" id="code_num" name="code_num" maxlength="4" /> <img src="http://www.gimoo.net/t/1512/code_num.php" id="getcode_num" title="看不清,點擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_num" value="提交" /></p>
 </div>
 <div class="demo">
 <h3>2、數字+字母驗證碼</h3>
 <p>驗證碼:<input type="text" class="input" id="code_char" maxlength="4" /> <img src="http://www.gimoo.net/t/1512/code_char.php" id="getcode_char" title="看不清,點擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_char" value="提交" /></p>
 </div>
 <div class="demo">

 <h3>3、中文驗證碼</h3>
 <p>驗證碼:<input type="text" class="input" id="code_zh" maxlength="4" /> <img src="http://www.gimoo.net/t/1512/code_zh.php" id="getcode_zh" title="看不清,點擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_zh" value="提交" /></p>
 </div>
 <div class="demo">

 <h3>4、仿google驗證碼</h3>
 <p>驗證碼:<input type="text" class="input" id="code_gg" maxlength="4" /> <img src="http://www.gimoo.net/t/1512/code_gg.php" id="getcode_gg" title="看不清,點擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_gg" value="提交" /></p>
 </div>
 <div class="demo">

<h3>5、算術驗證碼</h3>
 <p>驗證碼:<input type="text" class="input" id="code_math" maxlength="4" /> <img src="http://www.gimoo.net/t/1512/code_math.php" id="getcode_math" title="看不清,點擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_math" value="提交" /></p>
</div>

2、js驗證

$(function() {
 $("#getcode_num").click(function() { //數字驗證
 $(this).attr("src", 'code_num.php?' + Math.random());
 });
 $("#chk_num").click(function() {
 var code_num = $("#code_num").val();
 $.post("chk_code.php?act=num", {
  code: code_num
 },
 function(msg) {
  if (msg == 1) {
  alert("驗證碼正確!");
  } else {
  alert("驗證碼錯誤!");
  }
 });
 });

//數字+字母驗證

 $("#getcode_char").click(function() {
 $(this).attr("src", 'code_char.php?' + Math.random());
 });
 $("#chk_char").click(function() {
 var code_char = $("#code_char").val();
 $.post("chk_code.php?act=char", {
  code: code_char
 },
 function(msg) {
  if (msg == 1) {
  alert("驗證碼正確!");
  } else {
  alert("驗證碼錯誤!");
  }
 });
 });

//中文驗證碼

 $("#getcode_zh").click(function() {
 $(this).attr("src", 'code_zh.php?' + Math.random());
 });
 $("#chk_zh").click(function() {
 var code_zh = escape($("#code_zh").val());
 $.post("chk_code.php?act=zh", {
  code: code_zh
 },
 function(msg) {
  if (msg == 1) {
  alert("驗證碼正確!");
  } else {
  alert("驗證碼錯誤!");
  }
 });
 });

//google驗證

 $("#getcode_gg").click(function() {
 $(this).attr("src", 'code_gg.php?' + Math.random());
 });
 $("#chk_gg").click(function() {
 var code_gg = $("#code_gg").val();
 $.post("chk_code.php?act=gg", {
  code: code_gg
 },
 function(msg) {
  if (msg == 1) {
  alert("驗證碼正確!");
  } else {
  alert("驗證碼錯誤!");
  }
 });
 });

//算術驗證

 $("#getcode_math").click(function() {
 $(this).attr("src", 'code_math.php?' + Math.random());
 });
 $("#chk_math").click(function() {
 var code_math = $("#code_math").val();
 $.post("chk_code.php?act=math", {
  code: code_math
 },
 function(msg) {
  if (msg == 1) {
  alert("驗證碼正確!");
  } else {
  alert("驗證碼錯誤!");
  }
 });
 });
});

3、PHP生成驗證碼

session_start();
getCode(4,60,20);
function getCode($num,$w,$h) {
 $code = "";
 for ($i = 0; $i < $num; $i++) {
 $code .= rand(0, 9);
 }
 //4位驗證碼也可以用rand(1000,9999)直接生成
 //將生成的驗證碼寫入session,備驗證時用
 $_SESSION["helloweba_num"] = $code;
 //創建圖片,定義顏色值
 header("Content-type: image/PNG");
 $im = imagecreate($w, $h);
 $black = imagecolorallocate($im, 0, 0, 0);
 $gray = imagecolorallocate($im, 200, 200, 200);
 $bgcolor = imagecolorallocate($im, 255, 255, 255);
 //填充背景
 imagefill($im, 0, 0, $gray);
 //畫邊框
 imagerectangle($im, 0, 0, $w-1, $h-1, $black);
 //隨機繪制兩條虛線,起干擾作用
 $style = array ($black,$black,$black,$black,$black,
 $gray,$gray,$gray,$gray,$gray
 );
 imagesetstyle($im, $style);
 $y1 = rand(0, $h);
 $y2 = rand(0, $h);
 $y3 = rand(0, $h);
 $y4 = rand(0, $h);
 imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
 imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);
 //在畫布上隨機生成大量黑點,起干擾作用;
 for ($i = 0; $i < 80; $i++) {
 imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
 }
 //將數字隨機顯示在畫布上,字符的水平間距和位置都按一定波動范圍隨機生成
 $strx = rand(3, 8);
 for ($i = 0; $i < $num; $i++) {
 $strpos = rand(1, 6);
 imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
 $strx += rand(8, 12);
 }
 imagepng($im);//輸出圖片
 imagedestroy($im);//釋放圖片所占內存
}

以上內容就是php生成圖片驗證碼-附五種驗證碼的全部內容,希望大家喜歡。



主站蜘蛛池模板: 我爱我爹全集高清版免费观看| 打龙袍全集免费观看| 我的1919 电影| 年会不能停豆瓣评分| 王艺潼| chinesehd国语话对白| 电影《uhaw》免费观看| 拨萝卜电视剧视频歌高清在线观看大牛 | tvb翡翠台直播| 妹妹扮演的角色| 林智妍三部曲电影免费观看| 郑艳丽曹查理主演的影片| 国家宝藏之觐天宝匣 电视剧| 李尸朝鲜第三季| 曹永廉| 抖音客户端| 情动电影| 入党培养考察情况范文| 浪人街| 宇宙巨人希曼| 杨玉环一级片| 美女操帅哥| 诱惑热舞| 白宝山电视剧26集免费观看| 风间由美电影影片| 尼格| 那些年,那些事 电视剧| 深流 电视剧| 女用春情药什么好| 中央八套电视剧| 蓝家宝电影| 石璐| 黑暗女监日本电影完整版叫什么| 妻子的电影| 少儿不宜视频| 暗夜尖叫1988美国版高清观看| 怆然的读音| 俺去也电影网| 脓毒血症护理查房ppt| 《狼狈》电影| 路易斯·帕特里奇|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103