程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php生成驗證碼與驗證碼驗證完整實例

php生成驗證碼與驗證碼驗證完整實例

編輯:關於PHP編程

本文章分享一個自己使用的驗證碼實例,從生成圖片驗證碼到利用使用驗證碼的實例,有需要學習的同學可以參考一下本文章哦。

hyml頁面

 代碼如下 復制代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script language="javascript">
 function refresh_code()
 {
  form1.imgcode.src="verifycode.php?a="+Math.random();
 }
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="checkcode.php">
  <label for="code">驗證碼:</label>
  <input type="text" name="code" id="textfield" />
  <img id="imgcode" src="VerifyCode.php" alt="驗證碼" />
  <a href="javascript:refresh_code()">看不清?換一個</a>
  <input type="submit" name="button" id="button" value="提交" />
</form>
</body>
</html>

verifycode.php文件代碼如下

 代碼如下 復制代碼 <?php
 /*
  圖片驗證碼 Powered By KASON
   */
  session_start();
  $num=4;//驗證碼個數
  $width=80;//驗證碼寬度
  $height=20;//驗證碼高度
  $code=' ';
  for($i=0;$i<$num;$i++)//生成驗證碼
  {
   switch(rand(0,2))
   {
    case 0:$code[$i]=chr(rand(48,57));break;//數字
    case 1:$code[$i]=chr(rand(65,90));break;//大寫字母
    case 2:$code[$i]=chr(rand(97,122));break;//小寫字母
   }
  }
  $_SESSION["VerifyCode"]=$code;
  $image=imagecreate($width,$height);
  imagecolorallocate($image,255,255,255);
  for($i=0;$i<80;$i++)//生成干擾像素
  {
   $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
   imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);
  }
  for($i=0;$i<$num;$i++)//打印字符到圖像
  {
   $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
   imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color);
  }
  header("Content-type:image/png");
  imagepng($image);//輸出圖像到浏覽器
  imagedestroy($image);//釋放資源
?>   


  

checkcode.php文件如下

 代碼如下 復制代碼

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
ini_set('display_errors', 'Off');
session_start();
  if((strtoupper($_POST["code"])) == strtoupper(($_SESSION["VerifyCode"]))){
 print("驗證碼正確,");
  }else{
    print("驗證碼錯誤,");
  }
  echo "提交的驗證碼:".strtoupper($_POST["code"]).",正確的驗證碼:".strtoupper($_SESSION["VerifyCode"]);
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved