乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      php 驗證碼生成

       丶平上 2016-06-20

      現(xiàn)在來說說簡單的純數(shù)字驗證碼吧。

          如果是初學者,建議按照我代碼的注釋 //數(shù)字  一步步來。最簡單的方法,還是把整個代碼復(fù)制走了。

      新建一個captcha.php:

       //10>設(shè)置session,必須處于腳本最頂部
          session_start();
      
          $image = imagecreatetruecolor(100, 30);        //1>設(shè)置驗證碼圖片大小的函數(shù)
          //5>設(shè)置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
          $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
          //6>區(qū)域填充 int imagefill(int im, int x, int y, int col)  (x,y) 所在的區(qū)域著色,col 表示欲涂上的顏色
          imagefill($image, 0, 0, $bgcolor);
          //10>設(shè)置變量
          $captcha_code = "";
          //7>生成隨機數(shù)字
          for($i=0;$i<4;$i++){
              //設(shè)置字體大小
              $fontsize = 6;        
              //設(shè)置字體顏色,隨機顏色
              $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));            //0-120深顏色
              //設(shè)置數(shù)字
              $fontcontent = rand(0,9);
              //10>.=連續(xù)定義變量
              $captcha_code .= $fontcontent;    
              //設(shè)置坐標
              $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>增加干擾元素,設(shè)置雪花點
          for($i=0;$i<200;$i++){
              //設(shè)置點的顏色,50-200顏色比數(shù)字淺,不干擾閱讀
              $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));        
              //imagesetpixel — 畫一個單一像素
              imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
          }
          //9>增加干擾元素,設(shè)置橫線
          for($i=0;$i<4;$i++){
              //設(shè)置線的顏色
              $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));
              //設(shè)置線,兩點一線
              imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
          }
      
          //2>設(shè)置頭部,image/png
          header('Content-Type: image/png');
          //3>imagepng() 建立png圖形函數(shù)
          imagepng($image);
          //4>imagedestroy() 結(jié)束圖形函數(shù)  銷毀$image
          imagedestroy($image);


      接著就是靜態(tài)頁的代碼了:index.html
      <html>
          <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>    
          body>
      </html>

      從index.html可以看到,提交的表單是到form.php的,所以還要有一個判斷的form.php代碼:


       header("Content-Type:text/html;charset=utf-8");            //設(shè)置頭部信息
          //isset()檢測變量是否設(shè)置
          if(isset($_REQUEST['authcode'])){
              session_start();
              //strtolower()小寫函數(shù)
              if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){
                  //跳轉(zhuǎn)頁面
                  echo "<script language=\"javascript\">";
                  echo "document.location=\"./form.php\"";
                  echo "</script>";
              }else{
                  //提示以及跳轉(zhuǎn)頁面
                  echo "<script language=\"javascript\">";
                  echo "alert('輸入錯誤!');";
                  echo "document.location=\"./form.php\"";
                  echo "</script>";
              }
              exit();
          }


        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約