久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php如何實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼

      php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的方法:【session_start(); $width = 150; $height = 40; $image = imagecreatetruecolor($width, $height);$bgcol…】。

      php如何實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼

      本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。

      現(xiàn)在驗(yàn)證碼在表單中的應(yīng)用越來越多了,但是如果用js來實(shí)現(xiàn)總覺得不太方便,因此使用php來實(shí)現(xiàn)下,在此記錄下。

      當(dāng)然,我們也可以封裝成一個(gè)函數(shù),以后使用的時(shí)候也是很方便的,這里并未封裝,感興趣的小伙伴可以自己封裝下。

      具體實(shí)現(xiàn)代碼:

      新建一個(gè)cap_sz.php文件:

      <?php session_start(); //設(shè)置session,一定要在頂部  $width = 150; //設(shè)置圖片寬為300像素 $height = 40; //設(shè)置圖片高為40像素  $image = imagecreatetruecolor($width, $height); //設(shè)置驗(yàn)證碼大小的函數(shù) $bgcolor = imagecolorallocate($image, 255, 255, 255); //驗(yàn)證碼顏色RGB為(255,255,255)#ffffff imagefill($image, 0, 0, $bgcolor); //區(qū)域填充  $cap_code = ""; for($i=0;$i<4;$i++){     $fontsize = 7; //設(shè)置字體大小     $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));     //數(shù)字越大,顏色越淺,這里是深顏色0-120     $fontcontent = rand(0,9);     $cap_code.=$fontcontent; //.=連續(xù)定義變量         $x = ($i*150/4)+rand(5,10);     $y = rand(5,10);     //設(shè)置坐標(biāo)         imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); }  $_SESSION['code'] = $cap_code; //存到session  //設(shè)置干擾元素,設(shè)置雪花點(diǎn) for($i=0;$i<300;$i++){     $inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));     //設(shè)置顏色,20-200顏色比數(shù)字淺,不干擾閱讀     imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);     //畫一個(gè)單一像素的元素 }  //增加干擾元素,設(shè)置橫線(先設(shè)置線的顏色,在設(shè)置橫線) for ($i=0;$i<4;$i++) {     $linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));     //設(shè)置線的顏色      imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);    }  //因?yàn)橛行g覽器,訪問的content-type會(huì)是文本型(亂碼),所以我們需要設(shè)置成圖片的格式類型 header('Content-Type:image/png');  imagepng($image); //建立png函數(shù) imagedestroy($image); //結(jié)束圖形函數(shù),消除$image

      然后新建一個(gè)index.php進(jìn)行驗(yàn)證

      <?php header("Content-Type: text/html;charset=utf-8"); if (isset($_REQUEST['code'])){     session_start();      if ($_REQUEST['code'] == $_SESSION['code']){         echo "輸入正確";     }else{         echo "輸入錯(cuò)誤,請重新輸入";     }      exit(); }  ?>  <!DOCTYPE html> <html> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> //這里不能少,不然亂碼     <title>驗(yàn)證碼測試</title> </head> <body> <form> <p>驗(yàn)證碼:<img src="cap_sz.php" onClick="this.src='cap_sz.php?nocache='+Math.random()" style="cursor:hand" alt="點(diǎn)擊換一張"/>點(diǎn)擊圖片可更換驗(yàn)證碼</p> <p>請輸入圖片中的內(nèi)容:<input type="text" name="code" value=""/></p> <p><input type="submit" width="20px" height=19px value="提交"></input></p>  </form> </body> </html>

      推薦學(xué)習(xí):php培訓(xùn)

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)