php寫一個純數字驗證碼教學

來源:互聯網
上載者:User
現在來說說簡單的純數字驗證碼吧。

如果是初學者,建議按照我代碼的注釋 //數字 一步步來。最簡單的方法,還是把整個代碼複製走了。

建立一個captcha.php:

<?php  //11>設定session,必須處於指令碼最頂部  session_start();     /*$image = imagecreatetruecolor(100, 30);    //1>設定驗證碼圖片大小的函數  //5>設定驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);  $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff  //6>地區填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的地區著色,col 表示欲塗上的顏色  imagefill($image, 0, 0, $bgcolor);  //10>設定變數  $captcha_code = "";*/        //7>產生隨機數字  for($i=0;$i<4;$i++){    //設定字型大小    $fontsize = 6;        //設定字型顏色,隨機顏色    $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));      //0-120深顏色    //設定數字    $fontcontent = rand(0,9);    //10>.=連續定義變數    $captcha_code .= $fontcontent;      //設定座標    $x = ($i*100/4)+rand(5,10);    $y = rand(5,10);     imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);  }  //10>存到session  $_SESSION['authcode'] = $captcha_code;  //8>增加幹擾元素,設定雪花點  for($i=0;$i<200;$i++){    //設定點的顏色,50-200顏色比數字淺,不干擾閱讀    $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));        //imagesetpixel — 畫一個單一像素    imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);  }  //9>增加幹擾元素,設定橫線  for($i=0;$i<4;$i++){    //設定線的顏色    $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));    //設定線,兩點一線    imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);  }   //2>設定頭部,image/png  header('Content-Type: image/png');  //3>imagepng() 建立png圖形函數  imagepng($image);  //4>imagedestroy() 結束圖形函數 銷毀$image  imagedestroy($image); 接著就是靜態頁的代碼了:index.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "、www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns=" <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><form method="post" action="./form.php">  <p>驗證碼: <img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?>' style="width:100px; height:30px" />    <a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">換一個?</a>  </p>  <P>請輸入驗證碼:<input type="text" name='authcode' value=''/></p>  <p><input type='submit' value='提交' style='padding:6px 5px;'/></p></form></body></html>從index.html可以看到,提交的表單是到form.php的,所以還要有一個判斷的form.php代碼:<?php  header("Content-Type:text/html;charset=utf-8");      //設定頭部資訊  //isset()檢測變數是否設定  if(isset($_REQUEST['authcode'])){    session_start();    //strtolower()小寫函數    if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){      //跳轉頁面      echo "<script language=\"javascript\">";      echo "document.location=\"./form.php\"";      echo "</script>";    }else{      //提示以及跳轉頁面      echo "<script language=\"javascript\">";      echo "alert('輸入錯誤!');";      echo "document.location=\"./form.php\"";      echo "</script>";    }    exit();  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.