最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php生成圖片驗(yàn)證碼的實(shí)例講解

 更新時(shí)間:2015年08月03日 16:33:34   投稿:lijiao  
本文將通過(guò)實(shí)例講解使用PHP生成圖片驗(yàn)證碼,并介紹生成驗(yàn)證碼的函數(shù),需要的朋友可以參考下

本文以實(shí)例演示5種驗(yàn)證碼,并介紹生成驗(yàn)證碼的函數(shù)。PHP生成驗(yàn)證碼的原理:通過(guò)GD庫(kù),生成一張帶驗(yàn)證碼的圖片,并將驗(yàn)證碼保存在Session中。

1、HTML
5中驗(yàn)證碼HTML代碼如下:

<div class="demo">
 <h3>1、數(shù)字驗(yàn)證碼</h3>
 <p>驗(yàn)證碼:<input type="text" class="input" id="code_num" name="code_num" maxlength="4" /> <img src="code_num.php" id="getcode_num" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_num" value="提交" /></p>
 </div>
 <div class="demo">
 <h3>2、數(shù)字+字母驗(yàn)證碼</h3>
 <p>驗(yàn)證碼:<input type="text" class="input" id="code_char" maxlength="4" /> <img src="code_char.php" id="getcode_char" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_char" value="提交" /></p>
 </div>
 <div class="demo">
 <h3>3、中文驗(yàn)證碼</h3>
 <p>驗(yàn)證碼:<input type="text" class="input" id="code_zh" maxlength="4" /> <img src="code_zh.php" id="getcode_zh" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_zh" value="提交" /></p>
 </div>
 <div class="demo">
 <h3>4、仿google驗(yàn)證碼</h3>
 <p>驗(yàn)證碼:<input type="text" class="input" id="code_gg" maxlength="4" /> <img src="code_gg.php" id="getcode_gg" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_gg" value="提交" /></p>
 </div>
 <div class="demo">
 <h3>5、算術(shù)驗(yàn)證碼</h3>
 <p>驗(yàn)證碼:<input type="text" class="input" id="code_math" maxlength="4" /> <img src="code_math.php" id="getcode_math" title="看不清,點(diǎn)擊換一張" align="absmiddle" /></p>
 <p><input type="button" class="btn" id="chk_math" value="提交" /></p>
</div>

2、js驗(yàn)證

$(function() {
 $("#getcode_num").click(function() { //數(shù)字驗(yàn)證
  $(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("驗(yàn)證碼正確!");
   } else {
    alert("驗(yàn)證碼錯(cuò)誤!");
   }
  });
 });
 //數(shù)字+字母驗(yàn)證
 $("#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("驗(yàn)證碼正確!");
   } else {
    alert("驗(yàn)證碼錯(cuò)誤!");
   }
  });
 });
 //中文驗(yàn)證碼
 $("#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("驗(yàn)證碼正確!");
   } else {
    alert("驗(yàn)證碼錯(cuò)誤!");
   }
  });
 });
 //google驗(yàn)證
 $("#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("驗(yàn)證碼正確!");
   } else {
    alert("驗(yàn)證碼錯(cuò)誤!");
   }
  });
 });
 //算術(shù)驗(yàn)證
 $("#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("驗(yàn)證碼正確!");
   } else {
    alert("驗(yàn)證碼錯(cuò)誤!");
   }
  });
 });
});

3、PHP生成驗(yàn)證碼

session_start();
getCode(4,60,20);

function getCode($num,$w,$h) {
 $code = "";
 for ($i = 0; $i < $num; $i++) {
  $code .= rand(0, 9);
 }
 //4位驗(yàn)證碼也可以用rand(1000,9999)直接生成
 //將生成的驗(yàn)證碼寫入session,備驗(yàn)證時(shí)用
 $_SESSION["helloweba_num"] = $code;
 //創(chuàng)建圖片,定義顏色值
 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);

 //隨機(jī)繪制兩條虛線,起干擾作用
 $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);

 //在畫布上隨機(jī)生成大量黑點(diǎn),起干擾作用;
 for ($i = 0; $i < 80; $i++) {
  imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
 }
 //將數(shù)字隨機(jī)顯示在畫布上,字符的水平間距和位置都按一定波動(dòng)范圍隨機(jī)生成
 $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);//釋放圖片所占內(nèi)存
}

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

相關(guān)文章

最新評(píng)論

青河县| 茌平县| 台北县| 罗山县| 眉山市| 女性| 沙雅县| 彩票| 嘉荫县| 丹巴县| 驻马店市| 唐海县| 夏河县| 聂荣县| 白河县| 昌江| 贵阳市| 韶关市| 宜阳县| 象山县| 许昌县| 富阳市| 乐都县| 巨鹿县| 南宫市| 岢岚县| 陈巴尔虎旗| 四会市| 舒兰市| 共和县| 崇义县| 巴彦县| 米林县| 福海县| 巴青县| 建湖县| 治多县| 镇赉县| 成都市| 精河县| 济源市|