PHP之驗證碼制作

"驗證碼用于屏蔽機器寫入!!!"
目標:在底圖上顯示隨機內容(如數字)
方法:init imagecolorallocate ( resource $image , int $red , int $green , int $blue)
     bool imagestring ( resource $image , int $font , int $x , int $y , string $s ,  
 int $col)
注意事項:控制好字體與分布,避免字體重疊或顯示不全

目標:為驗證碼增加干擾元素,干擾的點或線(兩點確定一條線)
方法:bool imagesetpixel(resource $image,int $x,int $y,int $color)
    bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color)
注意事項:干擾信息一定控制好顏色,避免“喧賓奪主”

captcha.php:
<?php
  //生成一張背景圖片
  $image = imagecreatetruecolor(100,30);//創(chuàng)建一個寬100高30的區(qū)域
  $bgcolor = imagecolorallocate($image,255,255,255);//#ffffff
  imagefill($image,0,0,$bgcolor);//附上顏色
  //在底圖上生成隨機四個數字
  for( $i = 0; $i < 4; $i++) {
       $fontsize=6;
       $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
       $fontcontent=rand(0,9);
       $x=( $i * 100 / 4 )+rand(5,10);//設置x、y軸區(qū)間
       $y=rand(5,10);
       imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
   }
  
  //為底圖添加干擾點
  for( $i = 0; $i < 200; $i++) {
      $pointcolor = imagecolorallocate( $image, rand(50,200), rand(50,200), rand(50,200));
    imagesetpixel( $image, rand(1,99), rand(1, 29), $pointcolor);
  }
  //為底圖添加干擾線
  for( $i = 0; $i < 3; $i++) {
      $linecolor = imagecolorallocate( $image, rand(80,220), rand(80,220));
      imageline( $image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), linecolor);
  }

  header("Content-type: text/html; charset=utf-8");//亂碼問題
  header('content-type:image/png');
  imagepng($image);

  //end
  imagedestory($image);
  • php實現(xiàn)字母驗證碼
目標:讓圖片上的驗證碼內容顯示為字母,或數字、字母混合體
方法:int rand ( int $min, int $max )
mixed array_rand ( array $input [, int $num_req = 1] )
注意事項:N/A
for( $i = 0; $i < 4; $i++) {
    $fontsize = 6;
    $fontcolor = imagecolorallocate( $image, rand(0,120), rand(0,120),rand(0,120));
    $data = 'abcdefghijklmnopqrstuvwxyz123456789';
    $fontcontent = substr( $data,rand(0,strlen($data)-1), 1 );
    $x=( $i * 100 / 4 )+rand(5,10);//設置x、y軸區(qū)間
    $y=rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
 }
  • php通過session存儲驗證信息
目標:再服務器端記錄驗證碼信息,便于用戶輸入后做校驗
方法:bool session_start ( void )
注意事項:1. session_start()必須處于腳本最頂部
2. 多服務器情況,需要考慮幾種管理session信息
<?php
  session_start();

  $captch_code = '';
  for( $i = 0; $i < 4; $i++) {
    $fontsize = 6;
    $fontcolor = imagecolorallocate( $image, rand(0,120), rand(0,120),rand(0,120));
    $data = 'abcdefghijklmnopqrstuvwxyz123456789';
    $fontcontent = substr( $data,rand(0,strlen($data)-1), 1 );
    $x=( $i * 100 / 4 )+rand(5,10);//設置x、y軸區(qū)間
    $y=rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
 }
  //保存到session中指定字段里
  $_SESSION['authcode'] = $captch_code;
  • 驗證碼通過表單提交、校驗
目標:將已生成的驗證碼提供給用戶,并校驗用戶驗證碼的正確性
方法:HTML<form>表單基礎
注意事項:N/A
<?php
     if(isset($_REQUEST['authcode']))
     {
        session_start();
        //strtolower 轉化為小寫
        if (strtolower($_REQUEST['authcode'])==$_SESSION['authcode']) 
        {
            header('Content-type: text/html; charset=UTF8'); 
            echo '<font color="#0000CC">輸入正確</font>';
        }
        else{
            header('Content-type: text/html; charset=UTF8'); 
            echo '<font color="#CC0000"><b>輸入錯誤</b></font>';
            }
        exit();
     }
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/> 
    <title>確認驗證</title>
</head>
 <body>
  <form method="post" action="./form.php">
   <p>驗證碼圖片:<img id="captcha_img" border="1" src="./captcha.php?r=<?php echo rand();?>" width="100" height="30">
    <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 20px;"></p>
  </form>
 </body>
</html>
  • 使用js實現(xiàn)動態(tài)校驗驗證碼
1. 增加可點擊的“換一個”文案
2. 用js選取器選取驗證碼圖片
3. 用js修改驗證碼圖片地址(改src)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Php:腳本語言,網站建設,服務器端運行 PHP定義:一種服務器端的HTML腳本/編程語言,是一種簡單的、面向對象...
    廖馬兒閱讀 2,357評論 2 38
  • 22年12月更新:個人網站關停,如果仍舊對舊教程有興趣參考 Github 的markdown內容[https://...
    tangyefei閱讀 35,390評論 22 257
  • 背景 驗證碼就是把一串隨機產品的數字動態(tài)生成一幅圖片,再加上干擾元素。此時用戶可以通過肉眼能識別里面的數字或者字符...
    dy2903閱讀 2,345評論 0 7
  • 從三月份找實習到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍閱讀 42,787評論 11 349
  • 一天,周老板忽然收到綁匪的一個電話:“限你今天下午一點之前交出六萬元現(xiàn)金,否則,等著為你兒子周小小收尸吧!” 周老...
    探秘桂北閱讀 705評論 1 1

友情鏈接更多精彩內容