php產生驗證碼函數,php產生驗證碼_PHP教程

來源:互聯網
上載者:User

php產生驗證碼函數,php產生驗證碼


php產生驗證碼的函數,實用靠譜。先上下產生的驗證碼的(這裡產生的是全數位驗證碼的樣本效果):

下面是php產生驗證碼的源碼:

<?php session_start(); //session_register('CheckCode'); //PHP4.2以上版本不需要用session_register()註冊SESSION變數 $type='gif'; $width= 45; $height= 20; header("Content-type: image/".$type); srand((double)microtime()*1000000); if(isset($_GET['action'])){  $randval=randStr(4,$_GET['action']);  }else{  $randval=randStr(4,''); } if($type!='gif'&&function_exists('imagecreatetruecolor')){  $im=@imagecreatetruecolor($width,$height); }else{  $im=@imagecreate($width,$height); } $r=Array(225,211,255,223); $g=Array(225,236,237,215); $b=Array(225,236,166,125); $key=rand(0,3); $backColor=ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(隨機) $borderColor=ImageColorAllocate($im,127,157,185);//邊框色 $pointColor=ImageColorAllocate($im,255,170,255);//點顏色 @imagefilledrectangle($im,0,0,$width - 1,$height - 1,$backColor);//背景位置 @imagerectangle($im,0,0,$width-1,$height-1,$borderColor); //邊框位置 $stringColor=ImageColorAllocate($im,255,51,153); for($i=0;$i<=100;$i++){  $pointX=rand(2,$width-2);  $pointY=rand(2,$height-2);  @imagesetpixel($im,$pointX,$pointY,$pointColor); } @imagestring($im,5,5,1,$randval,$stringColor); $ImageFun='Image'.$type; $ImageFun($im); @imagedestroy($im); $_SESSION['CheckCode']=$randval; function randStr($len=6,$format='ALL'){  switch($format){   case 'ALL'://產生包含數字和字母的驗證碼    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;   case 'CHAR'://僅產生包含字母的驗證碼    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;   case 'NUMBER'://僅產生包含數位驗證碼    $chars='0123456789'; break;   default :    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;  }  $string='';  while(strlen($string)<$len)  $string.=substr($chars,(mt_rand()%strlen($chars)),1);  return $string; }

該函數的具體使用方法請看如下這個樣本(這裡是產生全數位驗證碼):

 

以上就是php如何產生驗證碼的實現函數,希望對大家的學習有所協助。

http://www.bkjia.com/PHPjc/1063237.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1063237.htmlTechArticlephp產生驗證碼函數,php產生驗證碼 php產生驗證碼的函數,實用靠譜。先上下產生的驗證碼的(這裡產生的是全數位驗證碼的樣本效...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.