一款php產生驗證碼實現代碼

來源:互聯網
上載者:User

session_save_path,可重新定義目錄。
*/
$sesssavepath = dirname(__file__)."/../data/sessions/";
if(is_writeable($sesssavepath) && is_readable($sesssavepath)){ session_save_path($sesssavepath); }
session_start();
$vstr = '';
for($i=0; $i<4; $i++) $vstr .= chr(mt_rand(65,90));
if(function_exists("imagecreate")){
 $ntime = time();
 if(empty($_session['verifycode_last']) || empty($_session['verifycode']) || ($ntime - $_session['verifycode_last'] > 5)){
  $_session['verifycode'] = strtolower($vstr);
  $_session['verifycode_last'] = $ntime;
 }
 $vstr = $_session['verifycode'];
 $vstrlen = strlen($vstr);
 $img = imagecreate(50,20);
 imagecolorallocate($img, 255,255,255);
 $line1 = imagecolorallocate($img,240,220,180);
 $line2 = imagecolorallocate($img,250,250,170);
 for($j=3;$j<=16;$j=$j+3){
  imageline($img,2,$j,48,$j,$line1);
 }
 for($j=2;$j<52;$j=$j+(mt_rand(3,6))){
  imageline($img,$j,2,$j-6,18,$line2);
 }
 $bordercolor = imagecolorallocate($img, 0x99,0x99,0x99);
 imagerectangle($img, 0, 0, 49, 19, $bordercolor);
 $fontcolor = imagecolorallocate($img, 48,61,50);
 for($i=0;$i<$vstrlen;$i++){
  $bc = mt_rand(0,1);
  $vstr[$i] = strtoupper($vstr[$i]);
  imagestring($img, 5, $i*10+6, mt_rand(2,4), $vstr[$i], $fontcolor);
 }

 header("pragma:no-cachern");
 header("cache-control:no-cachern");
 header("expires:0rn");

 if(function_exists("imagejpeg")){
  header("content-type:image/jpegrn");
  imagejpeg($img);
 }else{
  header("content-type:image/pngrn");
  imagepng($img);
 }
 imagedestroy($img);
 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.